﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Excel;
using System.Linq;
//取得过滤出来的行，批量修改可能会用到
//Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();


namespace BYManageInfoSystem
{
    public partial class Frm_RetrieveInfoGeneral : DockContent
    {
        CheckBoxOnHeader_CreationFilter aCheckBoxOnHeader_CreationFilter = new CheckBoxOnHeader_CreationFilter();

        private DGBY.BLL.FeeSJTruePayTab m_bllFeeSJTruePayTab = new DGBY.BLL.FeeSJTruePayTab();
        private bool m_bDateChangeValid = true;
        private DGBY.BLL.PaidanRegisterDetailTab m_bllPaidanRegisterDetailTab = new DGBY.BLL.PaidanRegisterDetailTab();
        private DGBY.BLL.DG_BackRecoveryDateCorrespondTab m_bllBackRecoveryDateCorrespondTab = new DGBY.BLL.DG_BackRecoveryDateCorrespondTab();
        private DGBY.BLL.BGPackingTab m_bllBGPackingTab = new DGBY.BLL.BGPackingTab();
        private bool m_bFilterTypeCheckedFlagLastTime = true;
        //private DGBY.BLL.BG_BGFeeTab m_bllBG_BGFeeTab = new DGBY.BLL.BG_BGFeeTab();
        //private DGBY.BLL.BG_GCFeeTab m_bllBG_GCFeeTab = new DGBY.BLL.BG_GCFeeTab();
        //private DGBY.BLL.BG_LGFeeTab m_bllBG_LGFeeTab = new DGBY.BLL.BG_LGFeeTab();
        //private DGBY.BLL.BG_TGDFeeTab m_bllBG_TGDFeeTab = new DGBY.BLL.BG_TGDFeeTab();
        //private DGBY.BLL.BG_GKJSFeeTab m_bllBG_GKJSFeeTab = new DGBY.BLL.BG_GKJSFeeTab();
        //private DGBY.BLL.BG_CGFeeTab m_bllBG_CGFeeTab = new DGBY.BLL.BG_CGFeeTab();
        //private DGBY.BLL.BG_JYFeeTab m_bllBG_JYFeeTab = new DGBY.BLL.BG_JYFeeTab();
        //private DGBY.BLL.BG_SMFeeTab m_bllBG_SMFeeTab = new DGBY.BLL.BG_SMFeeTab();


        private DGBY.BLL.FinCheckInvoiceRecordDetailTab_New m_bllFinCheckInvoiceRecordDetailTab_New = new DGBY.BLL.FinCheckInvoiceRecordDetailTab_New();
        private DGBY.BLL.FeeBGTruePayTab m_bllFeeBGTruePayTab = new DGBY.BLL.FeeBGTruePayTab();
        private DGBY.BLL.FacFeeTabNew m_bllFacFeeTabNew = new DGBY.BLL.FacFeeTabNew();
        private DGBY.BLL.DG_BackRecoveryMaterialResgistTab m_bllBackRecoveryMaterialResgistTab = new DGBY.BLL.DG_BackRecoveryMaterialResgistTab();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private bool m_bLastModifiedCellIsSelect = false;
        private int m_nDelIndex = -1;
        List<int> m_lstHideColIndex = new List<int>();
        private DGBY.BLL.BiZhiTab m_bllBiZhiTab = new DGBY.BLL.BiZhiTab();
        private UltraGridCell m_activeCell = null;
        private DGBY.BLL.JCHxdDjTab m_bllJCHxdDjTab = new DGBY.BLL.JCHxdDjTab();
        private DGBY.BLL.HxdNoTab m_bllHxdNoTab = new DGBY.BLL.HxdNoTab();
   //     private DGBY.BLL.BGDComeBackTab m_bllBGDComeBackTab = new DGBY.BLL.BGDComeBackTab();
        private int m_nSelectHxdNoTabID = 0;
        private UltraGridColumn ultraGridColumn1 = null;
        private readonly DGBY.BLL.BGCKHTTab m_bllBGCKHTTab = new DGBY.BLL.BGCKHTTab();
        Dictionary<int, int> m_dicSelectID2RowNo = new Dictionary<int, int>();
        //public static event EventHandler<RetrieveInfoGeneralEventArgs> onModify;

        //public void OnModify(RetrieveInfoGeneralEventArgs addEventArgs)
        //{
        //    if (onModify != null)
        //        onModify(this, addEventArgs);
        //}

        public void InitUltraWinGridSetting()
        {
            this.ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.VisibleIndex;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorWidth = 40;
        }

        public void LocalPrintPreveiw()
        {
            Infragistics.Shared.ResourceCustomizer rc = Infragistics.Win.Printing.Resources.Customizer;//Resources.Customizer;
            rc.SetCustomizedString("PrintPreview_DialogCaption", "打印预览");
            rc.SetCustomizedString("PrintPreview_Tool_Print", "打印(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_ClosePreview", "关闭(&C)");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuPreviewZoom", "显示比例");
            rc.SetCustomizedString("PrintPreview_Tool_Current_Page", "当前页");
            rc.SetCustomizedString("PrintPreview_Tool_Exit", "退出(&X)");
            rc.SetCustomizedString("PrintPreview_Tool_First_Page", "第一页");
            rc.SetCustomizedString("PrintPreview_Tool_Go_To", "跳至");
            rc.SetCustomizedString("PrintPreview_Tool_Last_Page", "最后一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_Page", "下一页");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_Page", "前一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_View", "下一视图(&N)");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_View", "前一视图(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_Hand_Tool", "手型工具(&H)");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Setup", "页面设置(&U)");
            rc.SetCustomizedString("PrintPreview_Tool_Snapshot_Tool", "快照工具(&S)");
            rc.SetCustomizedString("PrintPreview_Tool_View", "视图(&V)");
            rc.SetCustomizedString("PrintPreview_Tool_Whole_Page", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom", "缩放(&Z)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In", "放大");

            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out", "缩小");

            rc.SetCustomizedString("PrintPreview_ToolCategory_Context_Menus", "上下文菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_File", "文件");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Menus", "菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Tools", "工具栏");
            rc.SetCustomizedString("PrintPreview_ToolCategory_View", "视图");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Zoom_Mode", "缩放模式");

            rc.SetCustomizedString("PrintPreview_ToolTip_ClosePreview", "关闭");
            rc.SetCustomizedString("PrintPreview_ToolTip_Zoom", "缩放");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "页:{0}/{1}");

            rc.SetCustomizedString("CustomizeImg_ToolBar_MenuBar", "菜单");
            rc.SetCustomizedString("CustomizeImg_ToolBar_Standard", "标准");
            rc.SetCustomizedString("CustomizeImg_ToolBar_View", "视图");
            rc.SetCustomizedString("PrintPreview_Tool_File", "文件(&F)");
            rc.SetCustomizedString("PrintPreview_Tool_Tools", "工具(&T)");
            rc.SetCustomizedString("PrintPreview_Tool_Dynamic_Zoom_Tool", "动态缩放工具(&D)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out_Tool", "缩小工具");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In_Tool", "放大工具");
            // rc.SetCustomizedString("PrintPreview_Tool_Page_Layout","菜单");
            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PreviewRowColSelection_SelectedPages", "{0} x {1} 页");

            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Width", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_MarginWidth", "文字宽度");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_PageWidth", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_WholePage", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Layout", "页面布局");
            rc.SetCustomizedString("PrintPreview_Tool_Margin_Width", "文字宽度");
            rc.SetCustomizedString("ContextMenuPreviewHand", "缩放视图");


            rc.SetCustomizedString("PrintPreview_Tool_Reduce_Page_Thumbnails", "缩小");
            rc.SetCustomizedString("PrintPreview_Tool_Show_Page_Numbers", "显示页号");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuThumbnail", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Enlarge_Page_Thumbnails", "放大");
            rc.SetCustomizedString("PrintPreview_Tool_Thumbnails", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Continuous", "连续排序");
            //说明
            rc.SetCustomizedString("StatusBar_DynamicZoom_Instructions", "单击并拖动进行缩放操作");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "当前页: {0} / {1}");
            rc.SetCustomizedString("StatusBar_SnapShot_Instructions", "单击并拖动,系统将选定矩型区域复制到剪帖板");
            rc.SetCustomizedString("StatusBar_ZoomIn_Instructions", "单击并拖动,系统将放大选定矩型区域");
            rc.SetCustomizedString("StatusBar_ZoomOut_Instructions", "单击并拖动,系统将缩小选定矩型区域");
            rc.SetCustomizedString("StatusBar_Hand_Instructions", "单击并拖动以便显示更多内容");
        }

        public Frm_RetrieveInfoGeneral()
        {
            InitializeComponent();
        }
        private void InitDataTimeControlValue()
        {
            this.dateTimePickerInputDate.Value = DateTime.Parse("1900-01-01");
            this.dateTimePickerRecHXDDate.Value = DateTime.Parse("1900-01-01");
            this.dateTimePickerAlreadyHXDate.Value = DateTime.Parse("1900-01-01");
            //this.dateTimePickerInvoiceComeDate.Value = DateTime.Parse("1900-01-01");
            //this.dateTimePickerPostBuyGoodsContractDate.Value = DateTime.Parse("1900-01-01");
            this.dateTimePickerRecBuyGoodsContractDate.Value = DateTime.Parse("1900-01-01");
            this.dateTimePickerTaxBackInfoDate.Value = DateTime.Parse("1900-01-01");
            this.dateTimePickerShortOfSheetRegDate.Value = DateTime.Parse("1900-01-01");
            this.dateTimePickerGetSheetRegDate.Value = DateTime.Parse("1900-01-01");
            this.dateTimePickerGetCustomerContractRegDate.Value = DateTime.Parse("1900-01-01");
            //this.dateTimePickerNeedInfoRegDate.Value = DateTime.Parse("1900-01-01");
            //this.dateTimePickerGetInfoRegDate.Value = DateTime.Parse("1900-01-01");
            //this.dateTimePickerInfoErrorRegDate.Value = DateTime.Parse("1900-01-01");
            //this.dateTimePickerGetModifyInfoRegDate.Value = DateTime.Parse("1900-01-01");
            this.dateTimePicker1CustomerDate.Value = DateTime.Parse("1900-01-01");
            this.dateTimePickerDeclareDate.Value = DateTime.Parse("1900-01-01");
            this.dateTimePickerTongJi.Value = DateTime.Parse("1900-01-01");
            this.dateTimePickerError.Value = DateTime.Parse("1900-01-01");

        }
        private void Frm_RetrieveInfoGeneral_Load(object sender, EventArgs e)
        {
            this.ultraGrid1.CreationFilter = aCheckBoxOnHeader_CreationFilter;
            aCheckBoxOnHeader_CreationFilter.SetParam(m_dicSelectID2RowNo, this.ultraGrid1);
            this.comboBoxShowWay.Items.AddRange(new object[] { "最近半年记录", "全部记录" });
            this.comboBoxShowWay.SelectedIndex = 0;
            this.label4.Visible = Frm_Main.m_bCompleteVer;
            this.textBoxWeiTuoBook.Visible = Frm_Main.m_bCompleteVer;
            this.labelNote.Visible = Frm_Main.m_bCompleteVer;
            this.textBoxNote.Visible = Frm_Main.m_bCompleteVer;
           
            InitDataTimeControlValue();
            LocalPrintPreveiw();
            InitUltraWinGridSetting();
            DataSet ds = m_bllPaidanRegisterTab.GetNameAndID("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                StringBuilder str = new StringBuilder();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    str.Append("," + dr["合同编号"]);
                }
                string str2 = str.ToString().Substring(1);

                this.textBoxContractNo.AutoCompleteCustomSource.AddRange(str2.Split(','));
                this.textBoxContractNo.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
                this.textBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            }

            this.ck_wts.Checked = false;
            this.ck_lld.Checked = false;
            this.ck_tyd.Checked = false;
            this.ck_rcd.Checked = false;
            this.ck_fxt.Checked = false;
            this.ck_td.Checked = false;
            this.ck_ckht.Checked = false;
        }

        void ConvertStringToMultiLineFirst()
        {
            //foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //{
            //    string str = column2.Header.Caption;
            //    if (str == "集装箱柜号")
            //    {
            //        int nRowsCount = this.ultraGrid1.Rows.Count;
            //        for (int i = 0; i < nRowsCount; i++)
            //        {
            //            string strSource = this.ultraGrid1.Rows[i].GetCellValue(column2).ToString();
            //            ConvertStringToMultiLineSecond(strSource, i);
            //        }
            //    }
            //}
        }

        //private void ConvertStringToMultiLineSecond(string strSource, int nRowIndex)
        //{
        //    string str = strSource.ToString();
        //    string[] arr = str.Split(',');
        //    string tempa = "";
        //    int n = 0;
        //    foreach (string ch in arr)
        //    {

        //        if (ch != "")
        //        {
        //            if (n == 0)
        //                tempa += ch;
        //            else
        //                tempa += "\n" + ch;
        //        }

        //        n++;
        //    }

        //    this.ultraGrid1.Rows[nRowIndex].Cells["集装箱柜号"].Value = tempa;
        //}
        private string ConvertGuiNoString(string strSource)
        {
            string str = strSource.ToString();
            string[] arr = str.Split(',');
            string tempa = "";
            int n = 0;
            foreach (string ch in arr)
            {

                if (ch != "")
                {
                    if (n == 0)
                        tempa += ch;
                    else
                        tempa += "\n" + ch;
                }

                n++;
            }

            return tempa;
        }
        //private void ProcessMultiHeader()
        //{
        //    int nBizhiCount = 0;
        //    DataSet ds = m_bllBiZhiTab.GetList("");
        //    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        //    {
        //        nBizhiCount = ds.Tables[0].Rows.Count;
        //    }

        //    int nSpanX = 0;
        //    int nOriginX = 0;
        //    int nOriginXTemp = 0;
        //    bool bSpan = false;
        //    foreach (UltraGridColumn column2 in this.ultraGrid1.DisplayLayout.Bands[0].Columns)
        //    {

        //        string str = column2.Header.Caption;
        //        if (str == "工厂名称")
        //            bSpan = false;

        //        if (!bSpan)
        //        {
        //            if (str == "报关单金额")
        //                continue;
        //            column2.RowLayoutColumnInfo.SpanY = 4;
        //            if(column2.Hidden == false)
        //                nOriginXTemp += 2;
        //        }
        //        else
        //        {
        //            nBizhiCount--;
        //            if (nSpanX == 0 && nBizhiCount == 0)
        //                return;
        //            if (column2.Hidden == false)
        //            {
        //                nSpanX += 2;
        //                column2.RowLayoutColumnInfo.OriginY = 2;
        //            }
        //        }
        //        if (str == "PaidanRegisterTabID")
        //        {
        //            bSpan = true;
        //            nOriginX = nOriginXTemp;
        //        }
        //    }

        //    ultraGridColumn1 = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("报关单金额", "报关单金额");
        //    ultraGridColumn1.CellActivation = Activation.Disabled;
        //    ultraGridColumn1.SortIndicator = SortIndicator.Disabled;
        //    ultraGridColumn1.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
        //    ultraGridColumn1.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
        //    ultraGridColumn1.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
        //    ultraGridColumn1.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
        //    ultraGridColumn1.RowLayoutColumnInfo.OriginY = 0;
        //    ultraGridColumn1.RowLayoutColumnInfo.SpanX = nSpanX;
        //    ultraGridColumn1.RowLayoutColumnInfo.SpanY = 2;
        //}

        //ultraGrid1.DisplayLayout.Bands[0].UseRowLayout = true;
        //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
        //因为要处理冻结列，所以这里就不能多表头了
        private void ProcessMultiHeader()
        {
            //int nSpanX = 0;
            //int nOriginX = 0;
            //int nOriginXTemp = 0;
            //bool bSpan = false;
            //foreach (UltraGridColumn column2 in this.ultraGrid1.DisplayLayout.Bands[0].Columns)
            //{
            //    string str = column2.Header.Caption;
            //    if (str == "工厂名称")
            //        bSpan = false;
            //    if (!bSpan)
            //    {
            //        if (str == "报关单金额")
            //            continue;
            //        column2.RowLayoutColumnInfo.SpanY = 4;
            //        if (column2.Hidden == false)
            //            nOriginXTemp += 2;
            //    }
            //    else
            //    {
            //        nSpanX += 2;
            //        column2.RowLayoutColumnInfo.OriginY = 2;
            //    }
            //    if (str == "PaidanRegisterTabID")
            //    {
            //        bSpan = true;
            //        nOriginX = nOriginXTemp;
            //    }
            //}

            //ultraGridColumn1 = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("报关单金额", "报关单金额");
            //ultraGridColumn1.CellActivation = Activation.Disabled;
            //ultraGridColumn1.SortIndicator = SortIndicator.Disabled;
            //ultraGridColumn1.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            //ultraGridColumn1.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            //ultraGridColumn1.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            //ultraGridColumn1.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
            //ultraGridColumn1.RowLayoutColumnInfo.OriginY = 0;
            //ultraGridColumn1.RowLayoutColumnInfo.SpanX = nSpanX;
            //ultraGridColumn1.RowLayoutColumnInfo.SpanY = 2;
        }
        private int FindInsertIndex(DataSet ds)
        {
            for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
            {
                string str = ds.Tables[0].Columns[j].Caption;


                if (str == "审票日期")
                {
                    return j+1;
                }
  
            }

            return 0;
        }
        private void AddJiaShui(DataSet ds)
        {
            int nIndex = FindInsertIndex(ds);
            ds.Tables[0].Columns.Add("价税合计", System.Type.GetType("System.Decimal"));
            ds.Tables[0].Columns["价税合计"].SetOrdinal(nIndex++);
            ds.Tables[0].Columns.Add("退税金额", System.Type.GetType("System.Decimal"));
            ds.Tables[0].Columns["退税金额"].SetOrdinal(nIndex);

            ds.Tables[0].Columns[nIndex].SetOrdinal(nIndex++);


            DataSet dsTemp = m_bllBGCKHTTab.GetFinBuyGoodsContractDetailListWithPaidanRegisterTabID("");
            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
            {
                //  

                string strContractNo = ds.Tables[0].Rows[j]["合同编号"].ToString();
                if (strContractNo == "")
                    continue;
                //DataSet dsTemp = m_bllBGCKHTTab.GetFinBuyGoodsContractDetailListWithPaidanRegisterTabID("");
                if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
                {
                    DataRow[] Rows = dsTemp.Tables[0].Select("ContractCode = '" + strContractNo + "'");

                    Decimal dtJiaTaxTemp = 0;
                    Decimal dtJiaTaxSum = 0;
                    Decimal dtBackTaxJinE = 0;
                    for (int i = 0; i < Rows.Length; i++)
                    {
                        string strBizhi = Rows[0]["币制"].ToString();
                        Decimal dtBackTaxPoint = (Decimal)Rows[i]["退税点"];
                        Decimal dtFactorA = (Decimal)Rows[i]["系数A"];
                        Decimal dtHuiLv = (Decimal)Rows[i]["开票汇率"];
                        Decimal dtHKHuiLv = (Decimal)Rows[i]["港币开票汇率"];
                        int nFormulaSelector = (int)Rows[i]["价税合计公式选择"];

                        if (nFormulaSelector > 0 && strBizhi == "美元")
                        {
                            dtJiaTaxTemp = (Decimal)Rows[i]["报关金额"] * dtHuiLv * dtFactorA;
                            dtJiaTaxSum += (Decimal)Rows[i]["报关金额"] * dtHuiLv * dtFactorA;
                        }
                        else if (nFormulaSelector > 0 && strBizhi == "港币")
                        {
                            dtJiaTaxTemp = (Decimal)Rows[i]["报关金额"] * dtHKHuiLv * dtFactorA;
                            dtJiaTaxSum += (Decimal)Rows[i]["报关金额"] * dtHKHuiLv * dtFactorA;
                        }
                        else if (nFormulaSelector == 0 && strBizhi == "美元" && dtBackTaxPoint > 0)
                        {
                            dtJiaTaxTemp = (Decimal)Rows[i]["报关金额"] * (dtHuiLv - dtFactorA) * 1.17M / dtBackTaxPoint;
                            dtJiaTaxSum += (Decimal)Rows[i]["报关金额"] * (dtHuiLv - dtFactorA) * 1.17M / dtBackTaxPoint;
                        }
                        else if (nFormulaSelector == 0 && strBizhi == "港币" && dtHuiLv > 0 && dtBackTaxPoint > 0)
                        {
                            dtJiaTaxTemp = (Decimal)Rows[i]["报关金额"] * (dtHKHuiLv - dtFactorA * dtHKHuiLv / dtHuiLv) * 1.17M / dtBackTaxPoint;
                            dtJiaTaxSum += (Decimal)Rows[i]["报关金额"] * (dtHKHuiLv - dtFactorA * dtHKHuiLv / dtHuiLv) * 1.17M / dtBackTaxPoint;
                        }
                        else
                            dtJiaTaxSum += 0;

                        dtBackTaxJinE += (1.17M - dtBackTaxPoint) * dtJiaTaxTemp / 1.17M;
                    }

                    ds.Tables[0].Rows[j]["价税合计"] = dtJiaTaxSum;
                    ds.Tables[0].Rows[j]["退税金额"] = dtBackTaxJinE;

                }
            }
        }

        private void ShowAllRecord(string strWhere)
        {

         
            m_bllHxdNoTab.row2col();
            DataSet ds = m_bllHxdNoTab.ShowZhDjbRec(strWhere);

            //DataTable dtDispose = ultraGrid1.DataSource as DataTable;
            //if (dtDispose != null 
            //    && dtDispose.Rows.Count == ds.Tables[0].Rows.Count
            //    && this.checkBox1.Checked == m_bFilterTypeCheckedFlagLastTime
            //    && m_lstHideColIndex.Count == 0)
            //{
            //    foreach (Infragistics.Win.UltraWinGrid.UltraGridBand band in this.ultraGrid1.DisplayLayout.Bands)
            //    {
            //        // since all rows in a band have the same filters in RowFilterMode.AllRowsInBand this
            //        // will clear the filters
            //        band.ColumnFilters.ClearAllFilters();
                 
            //    }


            //    return;
            //    //dtDispose.Clear();
            //    //dtDispose.Dispose();
            //}

            ultraGrid1.DataSource = null;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                if (Frm_Main.m_bCompleteVer == false)
                {
                    ds.Tables[0].Columns["美元折扣"].ColumnName = "美元出口金额";
                    ds.Tables[0].Columns["港币折扣"].ColumnName = "港币出口金额";

                }

                AddJiaShui(ds);
                ultraGrid1.DataSource = ds.Tables[0];
                
                this.ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].CellMultiLine = Infragistics.Win.DefaultableBoolean.True;
                //ConvertStringToMultiLineFirst();
                
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[7].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["序号"].Hidden = true; 
                ultraGrid1.DisplayLayout.Bands[0].Columns["已核销日期"].Hidden = true;

                foreach (int nTemp in m_lstHideColIndex)
                {
                    if (nTemp < ultraGrid1.DisplayLayout.Bands[0].Columns.Count)
                        ultraGrid1.DisplayLayout.Bands[0].Columns[nTemp].Hidden = true;
                }

                m_lstHideColIndex.Clear();
                ProcessMultiHeader();
            }
            
            if (Frm_Main.m_bCompleteVer == false)
            {
              
                ultraGrid1.DisplayLayout.Bands[0].Columns["美元"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["港币"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["欧元折扣"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["新加元折扣"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["欧元"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["新加元"].Hidden = true;

                ultraGrid1.DisplayLayout.Bands[0].Columns["价税合计"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["退税金额"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["退税最后日期"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["委托书"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].Hidden = true;
            }

        }
        private void ShowAllRecordReLoad(string strWhere)
        {


            m_bllHxdNoTab.row2col();
            DataSet ds = m_bllHxdNoTab.ShowZhDjbRec(strWhere);

           

            ultraGrid1.DataSource = null;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                if (Frm_Main.m_bCompleteVer == false)
                {
                    ds.Tables[0].Columns["美元折扣"].ColumnName = "美元出口金额";
                    ds.Tables[0].Columns["港币折扣"].ColumnName = "港币出口金额";

                }

                AddJiaShui(ds);
                ultraGrid1.DataSource = ds.Tables[0];

                this.ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].CellMultiLine = Infragistics.Win.DefaultableBoolean.True;
                //ConvertStringToMultiLineFirst();

                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[7].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["序号"].Hidden = true;
                foreach (int nTemp in m_lstHideColIndex)
                {
                    if (nTemp < ultraGrid1.DisplayLayout.Bands[0].Columns.Count)
                        ultraGrid1.DisplayLayout.Bands[0].Columns[nTemp].Hidden = true;
                }
                m_lstHideColIndex.Clear();
                ProcessMultiHeader();
            }


            if (Frm_Main.m_bCompleteVer == false)
            {

                ultraGrid1.DisplayLayout.Bands[0].Columns["美元"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["港币"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["欧元折扣"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["新加元折扣"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["欧元"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["新加元"].Hidden = true;

                ultraGrid1.DisplayLayout.Bands[0].Columns["价税合计"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["退税金额"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["退税最后日期"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["委托书"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].Hidden = true;
            }
        }
        public static void ultraGrid1_InitializeRowFilter(Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();
            // FILTER ROW FUNCTIONALITY RELATED ULTRAGRID SETTINGS
            //e.Layout.Override.RowFilterAction = RowFilterAction.DisableFilteredOutRows;
            // ----------------------------------------------------------------------------------
            // Enable the the filter row user interface by setting the FilterUIType to FilterRow.
            e.Layout.Override.FilterUIType = FilterUIType.FilterRow;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // FilterEvaluationTrigger specifies when UltraGrid applies the filter criteria typed 
            // into a filter row. Default is OnCellValueChange which will cause the UltraGrid to
            // re-filter the data as soon as the user modifies the value of a filter cell.
            e.Layout.Override.FilterEvaluationTrigger = FilterEvaluationTrigger.OnCellValueChange;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // By default the UltraGrid selects the type of the filter operand editor based on
            // the column's DataType. For DateTime and boolean columns it uses the column's editors.
            // For other column types it uses the Combo. You can explicitly specify the operand
            // editor style by setting the FilterOperandStyle on the override or the individual
            // columns.
    
            e.Layout.Override.FilterOperandStyle = FilterOperandStyle.DropDownList;

            // By default UltraGrid displays user interface for selecting the filter operator. 
            // You can set the FilterOperatorLocation to hide this user interface. This
            // property is available on column as well so it can be controlled on a per column
            // basis. Default is WithOperand. This property is exposed off the column as well.
            e.Layout.Override.FilterOperatorLocation = FilterOperatorLocation.WithOperand;

            // By default the UltraGrid uses StartsWith as the filter operator. You use
            // the FilterOperatorDefaultValue property to specify a different filter operator
            // to use. This is the default or the initial filter operator value of the cells
            // in filter row. If filter operator user interface is enabled (FilterOperatorLocation
            // is not set to None) then that ui will be initialized to the value of this
            // property. The user can then change the operator as he/she chooses via the operator
            // drop down.
            e.Layout.Override.FilterOperatorDefaultValue = FilterOperatorDefaultValue.StartsWith;

            // FilterOperatorDropDownItems property can be used to control the options provided
            // to the user for selecting the filter operator. By default UltraGrid bases 
            // what operator options to provide on the column's data type. This property is
            // avaibale on the column as well.
            //e.Layout.Override.FilterOperatorDropDownItems = FilterOperatorDropDownItems.All;

            // By default UltraGrid displays a clear button in each cell of the filter row
            // as well as in the row selector of the filter row. When the user clicks this
            // button the associated filter criteria is cleared. You can use the 
            // FilterClearButtonLocation property to control if and where the filter clear
            // buttons are displayed.
            e.Layout.Override.FilterClearButtonLocation = FilterClearButtonLocation.RowAndCell;

            // Appearance of the filter row can be controlled using the FilterRowAppearance proeprty.
            e.Layout.Override.FilterRowAppearance.BackColor = Color.LightYellow;

            // You can use the FilterRowPrompt to display a prompt in the filter row. By default
            // UltraGrid does not display any prompt in the filter row.
            e.Layout.Override.FilterRowPrompt = "Click here to filter data...";

            // You can use the FilterRowPromptAppearance to change the appearance of the prompt.
            // By default the prompt is transparent and uses the same fore color as the filter row.
            // You can make it non-transparent by setting the appearance' BackColorAlpha property 
            // or by setting the BackColor to a desired value.
            e.Layout.Override.FilterRowPromptAppearance.BackColorAlpha = Alpha.Opaque;

            // By default the prompt is spread across multiple cells if it's bigger than the
            // first cell. You can confine the prompt to a particular cell by setting the
            // SpecialRowPromptField property off the band to the key of a column.
            //e.Layout.Bands[0].SpecialRowPromptField = e.Layout.Bands[0].Columns[0].Key;

            // Display a separator between the filter row other rows. SpecialRowSeparator property 
            // can be used to display separators between various 'special' rows, including for the
            // filter row. This property is a flagged enum property so it can take multiple values.
            e.Layout.Override.SpecialRowSeparator = SpecialRowSeparator.FilterRow;

            // You can control the appearance of the separator using the SpecialRowSeparatorAppearance
            // property.
            e.Layout.Override.SpecialRowSeparatorAppearance.BackColor = Color.FromArgb(233, 242, 199);

            //   UltraGridBand band = e.Layout.Bands[0];
            //   band.ColumnFilters["原币金额"].FilterConditions.Clear();
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.GreaterThan, 5);
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.LessThan, 10);
            //   band.ColumnFilters["原币金额"].LogicalOperator = FilterLogicalOperator.And;
        }
        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["报关单号"].Width = 130;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同数"].Width = 45;
            ultraGrid1.DisplayLayout.Bands[0].Columns["柜数"].Width = 45;
        }
        private void rowMergeView1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            InitColumnWidth();
            //ultraGrid1.DisplayLayout.Bands[0].UseRowLayout = true;
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
   
            //ultraGrid1.DisplayLayout.Bands[0].Columns["统计日期"].Header.Fixed = true;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["美元"].Header.Fixed = true;
            ultraGrid1.DisplayLayout.Override.FixedHeaderIndicator = FixedHeaderIndicator.None;  // 不显示小图标

            this.ultraGrid1.DisplayLayout.UseFixedHeaders = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["全选"].Header.Fixed = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同编号"].Header.Fixed = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["申报日期"].Header.Fixed = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["报关单号"].Header.Fixed = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["核销单号"].Header.Fixed = true;

            if (this.checkBox1.Checked == true)
                ultraGrid1_InitializeRowFilter(e);
            else
                ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);

            m_bFilterTypeCheckedFlagLastTime = this.checkBox1.Checked;
            e.Layout.Bands[0].Columns["全选"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;

         //   ultraGrid1.DisplayLayout.Bands[0].UseRowLayout = true;
           // e.Layout.Bands[0].Columns["统计日期"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DateTimeWithoutDropDown;
          //  e.Layout.Bands[0].Columns["统计日期"].Format = "{yyyy}";
           // rowMergeView1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in rowMergeView1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();

            List<string> lstTemp = new List<string>();
            lstTemp.Add("合同数");
            lstTemp.Add("柜数");
            lstTemp.Add("美元");
            lstTemp.Add("美元折扣");
            lstTemp.Add("港币");
            lstTemp.Add("港币折扣");
            ClassUltraGridCommonSet.SetSumColumn(e, lstTemp);
        }

        private void rowMergeView1_AfterCellActivate(object sender, EventArgs e)
        {
            //this.rowMergeView1.fi
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;
            m_activeCell = this.ultraGrid1.ActiveCell;
            this.labelCurrMod.Text = m_activeCell.Column.Header.Caption;
            this.labelCurrMod.ForeColor = Color.Red;
            string strHxdNoTabID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectHxdNoTabID = Convert.ToInt32(strHxdNoTabID, 10);
            cellHxdNoTabClick();
        }

        private void cellHxdNoTabClick()
        {
            DGBY.Model.HxdNoTab modelHxdNoTab = m_bllHxdNoTab.GetModel(m_nSelectHxdNoTabID);
            if (modelHxdNoTab != null)
            {
                m_bDateChangeValid = false;
                this.textBoxBGSheetNo.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["报关单号"].Text;
                this.textBoxHXDNo.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["核销单号"].Text; 
                this.textBoxNote.Text = modelHxdNoTab.Note;
                this.textBoxWeiTuoBook.Text = modelHxdNoTab.WeiTuoBook;
                
                this.textBoxContractNo.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["合同编号"].Text;


                this.dateTimePickerInputDate.Value = (System.DateTime)modelHxdNoTab.LrHsBgdDate;
                this.dateTimePickerRecHXDDate.Value = (System.DateTime)modelHxdNoTab.SdHxdDate;
                this.dateTimePickerAlreadyHXDate.Value = (System.DateTime)modelHxdNoTab.YhxDate;
                //this.dateTimePickerInvoiceComeDate.Value = (System.DateTime)modelHxdNoTab.HZzsInvDate;
                //this.dateTimePickerPostBuyGoodsContractDate.Value = (System.DateTime)modelHxdNoTab.JcGHHTDate;
                this.dateTimePickerRecBuyGoodsContractDate.Value = (System.DateTime)modelHxdNoTab.HsGHHTDate;
                this.dateTimePickerTaxBackInfoDate.Value = (System.DateTime)modelHxdNoTab.JcwTszldDate;
                //this.dateTimePickerOrderDate.Value = (System.DateTime)modelHxdNoTab.TuiShuiZHDate;
                this.dateTimePickerShortOfSheetRegDate.Value = (System.DateTime)modelHxdNoTab.QtddjDate;
                this.dateTimePickerGetSheetRegDate.Value = (System.DateTime)modelHxdNoTab.HstddjDate;
                this.dateTimePickerGetCustomerContractRegDate.Value = (System.DateTime)modelHxdNoTab.SCKHTdjDate;
                //this.dateTimePickerNeedInfoRegDate.Value = (System.DateTime)modelHxdNoTab.XbdzldjDate;
                this.dateTimePickerError.Value = (System.DateTime)modelHxdNoTab.HsbdzldjDate;
                //this.dateTimePickerInfoErrorRegDate.Value = (System.DateTime)modelHxdNoTab.ZlccdjDate;
                //this.dateTimePickerGetModifyInfoRegDate.Value = (System.DateTime)modelHxdNoTab.HsggzldjDate;

                this.dateTimePicker1CustomerDate.Value = (System.DateTime)modelHxdNoTab.CKouDate;
                this.dateTimePickerDeclareDate.Value = (System.DateTime)modelHxdNoTab.SBDate;
                this.dateTimePickerTongJi.Value = (System.DateTime)modelHxdNoTab.TongJDate;

                this.dateTimePickerHSLCD.Value = modelHxdNoTab.HSLCDDate != null ? (System.DateTime)modelHxdNoTab.HSLCDDate : DateTime.Parse("1900-01-01");
                this.dateTimePickerHSLLD.Value = modelHxdNoTab.HSLLDDate != null ? (System.DateTime)modelHxdNoTab.HSLLDDate : DateTime.Parse("1900-01-01"); 
                this.dateTimePickerHSTYD.Value = modelHxdNoTab.HSTYDDate != null ? (System.DateTime)modelHxdNoTab.HSTYDDate : DateTime.Parse("1900-01-01");

                //try
                //{
                //    if (this.rowMergeView1.Rows[this.rowMergeView1.ActiveCell.Row.Index].Cells["出口日期"].Value != null)
                //        this.dateTimePicker1CustomerDate.Value = (System.DateTime)this.rowMergeView1.Rows[this.rowMergeView1.ActiveCell.Row.Index].Cells["出口日期"].Value;
                //    if (this.rowMergeView1.Rows[this.rowMergeView1.ActiveCell.Row.Index].Cells["申报日期"].Value != null)
                //        this.dateTimePickerDeclareDate.Value = (System.DateTime)this.rowMergeView1.Rows[this.rowMergeView1.ActiveCell.Row.Index].Cells["申报日期"].Value;

                //}
                //catch (System.Exception e)
                //{

                //}
                m_bDateChangeValid = true;

                this.ck_wts.Checked = modelHxdNoTab.iswts;
                this.ck_lld.Checked = modelHxdNoTab.islld;
                this.ck_tyd.Checked = modelHxdNoTab.istyd;
                this.ck_rcd.Checked = modelHxdNoTab.isrcd;
                this.ck_fxt.Checked = modelHxdNoTab.isfxt;
                this.ck_td.Checked = modelHxdNoTab.istd;
                this.ck_ckht.Checked = modelHxdNoTab.isckht;
            }
        }
        /// <summary>
        /// 取得某月的最后一天
        /// </summary>
        /// <param name="datetime">要取得月份最后一天的时间</param>
        /// <returns></returns>
        private DateTime LastDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
        }
        //1.到月底的时候可能有几个合同号的核销单还没有回来，但是这几个合同号又要录入到综合登记表
        //填充一些时间字段。这时就拿临时的核销单》1000000001来绑定这些合同号（临时核销单号初始绑定
        //的合同号是“”，派单id是127
        //2.等这些合同号真正对应的核销单号回来后，在绑定这些合同号到真正的核销单号，同时系统删除旧的绑定。
        private void buttonMod_Click(object sender, EventArgs e)
        {
            try
            {
                           
                DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(m_nSelectHxdNoTabID);
                if (modelHxdNoTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (this.textBoxContractNo.Text == "")
                {
                    if (modelHxdNoTabOld.PaidanRegisterTabID == 127)
                    {
                        ModifyHxd(modelHxdNoTabOld, 127, false, null);
                        return;
                    }
                    else if (modelHxdNoTabOld.HxdNo >= 1000000001)
                    {
                        ModifyHxd(modelHxdNoTabOld, 127, false, null);
                        return;
                    }
                    MessageBox.Show("不能将合同号修改为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                 
                DataSet ds = m_bllPaidanRegisterTab.GetList(@"ContractCode = '" 
                    + this.textBoxContractNo.Text + "'");
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    MessageBox.Show("合同号不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                //如果是临时核销单号，则此时modelHxdNoTabOld.PaidanRegisterTabID = 127， strContractNoTemp = ""
                ds = m_bllPaidanRegisterTab.GetList(@"PaidanRegisterTabID = '" 
                    + modelHxdNoTabOld.PaidanRegisterTabID.ToString() + "'");

                string strContractNoTemp = "";
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    strContractNoTemp = ds.Tables[0].Rows[0]["ContractCode"].ToString();
                }


                if(strContractNoTemp != this.textBoxContractNo.Text)
                {
                     ds = m_bllHxdNoTab.ShowZhDjbRec("ContractCode = '" + this.textBoxContractNo.Text + "'"
+ "and HxdNoTab.HxdNo < '" + 1000000001 + "'");
                    //要修改成的合同号已经绑定了真正的核销单号
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        MessageBox.Show("合同号已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
            
               
                //此时要改成的合同号和原有的合同号可能等也可能不等

                int nPaidanRegisterTabID = 0;
                ds = m_bllHxdNoTab.ShowZhDjbRec("ContractCode = '" + this.textBoxContractNo.Text + "'"
 + "and HxdNoTab.HxdNo >= '" + 1000000001
+ "'" + " and HxdNoTab.HxdNo <= '" + 10000000001 + "'");
                //要修改成的合同号已经绑定了临时的核销单号
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 1)
                    {
                        MessageBox.Show("合同号已经存在啦!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    string strTemp = ds.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString();
                    //取得要修改成的合同号的派单id
                    if(strTemp != "")
                        nPaidanRegisterTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
                }

                //删除旧的绑定。
                ds = m_bllHxdNoTab.GetList("PaidanRegisterTabID = '" + nPaidanRegisterTabID + "'");
                int nHxdNo = Convert.ToInt32(this.textBoxHXDNo.Text);
                bool bModify = false;
                DGBY.Model.HxdNoTab modelHxdNoTabFalse = new DGBY.Model.HxdNoTab();
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if (nHxdNo < 1000000001)
                    {
                        if (MessageBox.Show("合同号已经存在,要调整合同号对应的核销单号么？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                        {
                            int nHxdNoTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["HxdNoTabID"].ToString(), 10);
                            modelHxdNoTabFalse = m_bllHxdNoTab.GetModel(nHxdNoTabID);
                           
                            m_bllHxdNoTab.Delete(nHxdNoTabID);
                            bModify = true;
                        }
                        else
                        {
                            return;
                        }
                    }
                  
                }

                //以下是修改核销单

                if (nPaidanRegisterTabID == 0)
                {
                    ds = m_bllPaidanRegisterTab.GetList("ContractCode = '" + this.textBoxContractNo.Text + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        nPaidanRegisterTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
                    }
                }

                if(ModifyHxd(modelHxdNoTabOld, nPaidanRegisterTabID, bModify, modelHxdNoTabFalse))
                    Frm_Main.Log2DB(this.Text, "修改", this.textBoxContractNo.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        /************************************************************/
        //k001 综合登记表修改日期   2012-8-22

        private void SyncDate(ref DGBY.Model.HxdNoTab modelHxdNoTabOld)
        {
            DataSet dsHxdNoTab = m_bllHxdNoTab.GetDateConfigInfoByBGH("HxdNoTab.HxdNoTabID = '" + m_nSelectHxdNoTabID.ToString() + "'");
            if (dsHxdNoTab.Tables.Count > 0 && dsHxdNoTab.Tables[0].Rows.Count > 0)
            {
                string strTemp = dsHxdNoTab.Tables[0].Rows[0]["委托书"].ToString();
                if (!string.IsNullOrEmpty(strTemp))
                {
                    bool bwts = Convert.ToBoolean(dsHxdNoTab.Tables[0].Rows[0]["委托书"].ToString());
                    if (bwts)
                    {
                        modelHxdNoTabOld.QtddjDate = modelHxdNoTabOld.LrHsBgdDate;
                    }
                }

                strTemp = dsHxdNoTab.Tables[0].Rows[0]["六联单"].ToString();
                if (!string.IsNullOrEmpty(strTemp))
                {
                    bool blld = Convert.ToBoolean(dsHxdNoTab.Tables[0].Rows[0]["六联单"].ToString());
                    if (blld)
                    {
                        modelHxdNoTabOld.HSLLDDate = modelHxdNoTabOld.LrHsBgdDate;
                    }
                }

                strTemp = dsHxdNoTab.Tables[0].Rows[0]["托运单"].ToString();
                if (!string.IsNullOrEmpty(strTemp))
                {
                    bool btyd = Convert.ToBoolean(dsHxdNoTab.Tables[0].Rows[0]["托运单"].ToString());
                    if (btyd)
                    {
                        modelHxdNoTabOld.HSTYDDate = modelHxdNoTabOld.LrHsBgdDate;
                    }
                }

                strTemp = dsHxdNoTab.Tables[0].Rows[0]["入仓单"].ToString();
                if (!string.IsNullOrEmpty(strTemp))
                {
                    bool brcd = Convert.ToBoolean(dsHxdNoTab.Tables[0].Rows[0]["入仓单"].ToString());
                    if (brcd)
                    {
                        modelHxdNoTabOld.HSLCDDate = modelHxdNoTabOld.LrHsBgdDate;
                    }
                }

                strTemp = dsHxdNoTab.Tables[0].Rows[0]["放行条"].ToString();
                if (!string.IsNullOrEmpty(strTemp))
                {
                    bool bfxt = Convert.ToBoolean(dsHxdNoTab.Tables[0].Rows[0]["放行条"].ToString());
                    if (bfxt)
                    {
                        modelHxdNoTabOld.HsbdzldjDate = modelHxdNoTabOld.LrHsBgdDate;
                    }
                }

                strTemp = dsHxdNoTab.Tables[0].Rows[0]["提单"].ToString();
                if (!string.IsNullOrEmpty(strTemp))
                {
                    bool btd = Convert.ToBoolean(dsHxdNoTab.Tables[0].Rows[0]["提单"].ToString());
                    if (btd)
                    {
                        modelHxdNoTabOld.HstddjDate = modelHxdNoTabOld.LrHsBgdDate;
                    }
                }

                strTemp = dsHxdNoTab.Tables[0].Rows[0]["出口合同"].ToString();
                if (!string.IsNullOrEmpty(strTemp))
                {
                    bool bckht = Convert.ToBoolean(dsHxdNoTab.Tables[0].Rows[0]["出口合同"].ToString());
                    if (bckht)
                    {
                        modelHxdNoTabOld.SCKHTdjDate = modelHxdNoTabOld.LrHsBgdDate;
                    }
                }
            }
        }
        /************************************************************/
        //同步备案资料
        private void UpdateBackRecoveryMaterialTab(DGBY.Model.HxdNoTab modelHxdNoTabOld, DateTime? dtHstddjDate, DateTime? dHSLCDDate,
           DateTime? dHSLLDDate, DateTime? dHSTYDDate, DateTime? dHsbdzldjDate, DateTime? dQtddjDate, DateTime? dSCKHTdjDate)
        {
            //判断回收时间是否允许修改
            if (modelHxdNoTabOld.HstddjDate != dtHstddjDate
                || modelHxdNoTabOld.HSLCDDate != dHSLCDDate
                || modelHxdNoTabOld.HSLLDDate != dHSLLDDate
                || modelHxdNoTabOld.HSTYDDate != dHSTYDDate
                || modelHxdNoTabOld.HsbdzldjDate != dHsbdzldjDate
                || modelHxdNoTabOld.QtddjDate != dQtddjDate
                || modelHxdNoTabOld.SCKHTdjDate != dSCKHTdjDate)
            {
                DGBY.Model.DG_BackRecoveryMaterialResgistTab modelBackRecoveryMaterialTab = m_bllBackRecoveryMaterialResgistTab.GetModelByPaidanRegisterTabID(Convert.ToInt32(modelHxdNoTabOld.PaidanRegisterTabID));
                if (null == modelBackRecoveryMaterialTab)
                    return;

                bool bNeedUpdate = false;
                if (1900 == dtHstddjDate.Value.Year && 1900 != modelHxdNoTabOld.HstddjDate.Value.Year)
                {
                    if (1 == modelBackRecoveryMaterialTab.td)
                    {
                        modelBackRecoveryMaterialTab.td = 4;
                        bNeedUpdate = true;
                    }
                }
                if (1900 == dHSLCDDate.Value.Year && 1900 != modelHxdNoTabOld.HSLCDDate.Value.Year)
                {
                    if (1 == modelBackRecoveryMaterialTab.rcd)
                    {
                        modelBackRecoveryMaterialTab.rcd = 4;
                        bNeedUpdate = true;
                    }
                }
                if (1900 == dHSLLDDate.Value.Year && 1900 != modelHxdNoTabOld.HSLLDDate.Value.Year)
                {
                    if (1 == modelBackRecoveryMaterialTab.lld)
                    {
                        modelBackRecoveryMaterialTab.lld = 4;
                        bNeedUpdate = true;
                    }
                }
                if (1900 == dHSTYDDate.Value.Year && 1900 != modelHxdNoTabOld.HSTYDDate.Value.Year)
                {
                    if (1 == modelBackRecoveryMaterialTab.tyd)
                    {
                        modelBackRecoveryMaterialTab.tyd = 4;
                        bNeedUpdate = true;
                    }
                }
                if (1900 == dHsbdzldjDate.Value.Year && 1900 != modelHxdNoTabOld.HsbdzldjDate.Value.Year)
                {
                    if (1 == modelBackRecoveryMaterialTab.fxt)
                    {
                        modelBackRecoveryMaterialTab.fxt = 4;
                        bNeedUpdate = true;
                    }
                }
                if (1900 == dQtddjDate.Value.Year && 1900 != modelHxdNoTabOld.QtddjDate.Value.Year)
                {
                    if (1 == modelBackRecoveryMaterialTab.wts)
                    {
                        modelBackRecoveryMaterialTab.wts = 4;
                        bNeedUpdate = true;
                    }
                }
                if (1900 == dSCKHTdjDate.Value.Year && 1900 != modelHxdNoTabOld.SCKHTdjDate.Value.Year)
                {
                    if (1 == modelBackRecoveryMaterialTab.ckht)
                    {
                        modelBackRecoveryMaterialTab.ckht = 4;
                        bNeedUpdate = true;
                    }
                }

                if (bNeedUpdate)
                {
                    m_bllBackRecoveryMaterialResgistTab.Update(modelBackRecoveryMaterialTab);
                    Frm_Main.Log2DB(this.Text, "同步备案资料", modelBackRecoveryMaterialTab.DG_BackRecoveryMaterialResgistTabID.ToString());
                }
            }
        }

        private bool ModifyHxd(DGBY.Model.HxdNoTab modelHxdNoTabOld
            , int nPaidanRegisterTabID, bool bModify, DGBY.Model.HxdNoTab modelHxdNoTabFalse)
        {
            DateTime dtNow = System.DateTime.Now.Date;

            modelHxdNoTabOld.PaidanRegisterTabID = nPaidanRegisterTabID;

           // modelHxdNoTabOld.Note = bModify ? modelHxdNoTabFalse.Note : this.textBoxNote.Text;
          //  modelHxdNoTabOld.WeiTuoBook = bModify ? modelHxdNoTabFalse.WeiTuoBook : this.textBoxWeiTuoBook.Text;

            modelHxdNoTabOld.Note = bModify ? modelHxdNoTabFalse.Note : this.textBoxNote.Text;
            modelHxdNoTabOld.WeiTuoBook = bModify ? modelHxdNoTabFalse.WeiTuoBook : this.textBoxWeiTuoBook.Text;
            //modelHxdNoTab.TuiShuiZHDate = this.dateTimePickerInputDate.Value;//有问题

            bool bModifySjShouldPay = false;
            DateTime? dtOldTongJDate = modelHxdNoTabOld.TongJDate;
            if (this.dateTimePickerTongJi.Value.Date.CompareTo(DateTime.Parse("1900-01-01")) > 0)
            {
                modelHxdNoTabOld.TongJDate = this.dateTimePickerTongJi.Value.Date;
            }
            else
            {
                if (modelHxdNoTabFalse != null && modelHxdNoTabFalse.TongJDate != null && modelHxdNoTabFalse.TongJDate.Value.Year > 1900)
                    modelHxdNoTabOld.TongJDate = modelHxdNoTabFalse.TongJDate;
                else
                    modelHxdNoTabOld.TongJDate = LastDayOfMonth(this.dateTimePickerDeclareDate.Value.Date);
            }

            if (dtOldTongJDate.Value.Date.Year == 1900 && modelHxdNoTabOld.TongJDate.Value.Date.Year != 1900)
            {
                bModifySjShouldPay = true;
            }

            modelHxdNoTabOld.CKouDate = this.dateTimePicker1CustomerDate.Value.Date;

            
            modelHxdNoTabOld.SBDate = this.dateTimePickerDeclareDate.Value.Date;
            if (this.dateTimePickerInputDate.Value.Date.CompareTo(DateTime.Parse("1900-01-01")) > 0)
                modelHxdNoTabOld.LrHsBgdDate = this.dateTimePickerInputDate.Value.Date;
            else
                modelHxdNoTabOld.LrHsBgdDate = dtNow;

            if (modelHxdNoTabOld.BGDNo != this.textBoxBGSheetNo.Text)
            {
                modelHxdNoTabOld.LrHsBgdDate = dtNow;
                DataSet ds = m_bllPaidanRegisterDetailTab.GetList(@"
                PaidanRegisterDetailTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'", false);
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    int nPaidanRegisterDetailTabID = (int)ds.Tables[0].Rows[0]["PaidanRegisterDetailTabID"];
                    DGBY.Model.PaidanRegisterDetailTab modelPaidanRegisterDetailTab
     = m_bllPaidanRegisterDetailTab.GetModel(nPaidanRegisterDetailTabID);

                    modelPaidanRegisterDetailTab.GouHuoContractDate = dtNow;
                    modelPaidanRegisterDetailTab.BusinessDate = dtNow;
                    modelPaidanRegisterDetailTab.BackTaxDate = dtNow;
                    m_bllPaidanRegisterDetailTab.Update(modelPaidanRegisterDetailTab);
                }
            }

            //获取修改前回收时间
            DateTime? dtHstddjDate = modelHxdNoTabOld.HstddjDate;
            DateTime? dHSLCDDate = modelHxdNoTabOld.HSLCDDate;
            DateTime? dHSLLDDate = modelHxdNoTabOld.HSLLDDate;
            DateTime? dHSTYDDate = modelHxdNoTabOld.HSTYDDate;
            DateTime? dHsbdzldjDate = modelHxdNoTabOld.HsbdzldjDate;
            DateTime? dQtddjDate = modelHxdNoTabOld.QtddjDate;
            DateTime? dSCKHTdjDate = modelHxdNoTabOld.SCKHTdjDate;

            modelHxdNoTabOld.BGDNo = this.textBoxBGSheetNo.Text;
            modelHxdNoTabOld.SdHxdDate = this.dateTimePickerRecHXDDate.Value.Date;
            modelHxdNoTabOld.YhxDate = this.dateTimePickerAlreadyHXDate.Value.Date;
     
            modelHxdNoTabOld.JcwTszldDate = this.dateTimePickerTaxBackInfoDate.Value.Date;


           
                    
            modelHxdNoTabOld.HsGHHTDate = bModify ? modelHxdNoTabFalse.HsGHHTDate : this.dateTimePickerRecBuyGoodsContractDate.Value.Date; 
            modelHxdNoTabOld.SCKHTdjDate = bModify ? modelHxdNoTabFalse.SCKHTdjDate : this.dateTimePickerGetCustomerContractRegDate.Value.Date;
            modelHxdNoTabOld.HZzsInvDate = DateTime.Parse("1900-01-01");
            modelHxdNoTabOld.JcGHHTDate = DateTime.Parse("1900-01-01");
            modelHxdNoTabOld.XbdzldjDate = DateTime.Parse("1900-01-01");
            //modelHxdNoTabOld.HsbdzldjDate = DateTime.Parse("1900-01-01");

            modelHxdNoTabOld.ZlccdjDate = modelHxdNoTabOld.SBDate.Value.Date.AddDays(45);//45天提醒
            modelHxdNoTabOld.HsggzldjDate = modelHxdNoTabOld.SBDate.Value.Date.AddDays(60);//60天提醒
            modelHxdNoTabOld.TuiShuiZHDate = this.dateTimePickerDeclareDate.Value.Date.AddDays(80);

            //修改单证日期
            CheckModifyBackRecoveryData(ref modelHxdNoTabOld, bModify, modelHxdNoTabFalse);

            modelHxdNoTabOld.iswts = this.ck_wts.Checked;
            modelHxdNoTabOld.islld = this.ck_lld.Checked;
            modelHxdNoTabOld.istyd = this.ck_tyd.Checked;
            modelHxdNoTabOld.isrcd = this.ck_rcd.Checked;
            modelHxdNoTabOld.isfxt = this.ck_fxt.Checked;
            modelHxdNoTabOld.istd = this.ck_td.Checked;
            modelHxdNoTabOld.isckht = this.ck_ckht.Checked;

            SyncDate(ref modelHxdNoTabOld);
            //if (!CheckHxdNoTabHSDate(modelHxdNoTabOld, dtHstddjDate, dHSLCDDate, dHSLLDDate, dHSTYDDate, dHsbdzldjDate, dQtddjDate))
            //    return false;

            m_bllHxdNoTab.Update(modelHxdNoTabOld);

            UpdateBackRecoveryMaterialTab(modelHxdNoTabOld, dtHstddjDate, dHSLCDDate, dHSLLDDate, dHSTYDDate, dHsbdzldjDate, dQtddjDate, dSCKHTdjDate);

            //RetrieveInfoGeneralEventArgs addEventArgs = new RetrieveInfoGeneralEventArgs();
            ////通知所有事件订阅者  
            //addEventArgs.m_nPaidanID = nPaidanRegisterTabID;

            //this.OnModify(addEventArgs);
            //ShowAllRecordInDataGridView();
            //if (modelHxdNoTabOld.CheckInvoiceDate == null)
            //    UpdateShenPiaoDateOnHxdTab(nPaidanRegisterTabID);

            Frm_Main.Log2DB(this.Text, "修改", this.textBoxContractNo.Text);
            SetCurrentUGR(modelHxdNoTabOld, this.textBoxContractNo.Text);
            //ShowAllRecord("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'");
            ProcessBGFee(nPaidanRegisterTabID, this.textBoxContractNo.Text);

            //修改商检应收费用计提年月
            if (bModifySjShouldPay)
            {
                ModifySjShouldPayMonth(nPaidanRegisterTabID, modelHxdNoTabOld.TongJDate);
            }

            MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return true;
        }

        private DGBY.Model.DG_BackRecoveryDateCorrespondTab GetBackRecoveryDateModelByPaidanID(int nPaidanRegisterTabID)
        {
            DGBY.Model.DG_BackRecoveryDateCorrespondTab modeBackRecoveryDate = new DGBY.Model.DG_BackRecoveryDateCorrespondTab();
            DataSet dsPacking = m_bllBGPackingTab.GetList("PaidanRegisterTabID = '" + nPaidanRegisterTabID + "'");
            if (dsPacking.Tables.Count > 0 && dsPacking.Tables[0].Rows.Count > 0)
            {
                int nHDataTabID = int.Parse(dsPacking.Tables[0].Rows[0]["BGHDataTabID"].ToString());
                modeBackRecoveryDate = m_bllBackRecoveryDateCorrespondTab.GetModelByBGHDataTabID(nHDataTabID);
            }

            return modeBackRecoveryDate;
        }

        private void CheckModifyBackRecoveryData(ref DGBY.Model.HxdNoTab modelHxdNoTabOld, bool bModify, DGBY.Model.HxdNoTab modelHxdNoTabFalse)
        {
            bool bUnModify = false;

            DGBY.Model.DG_BackRecoveryDateCorrespondTab modeBackRecoveryDate = GetBackRecoveryDateModelByPaidanID((int)modelHxdNoTabOld.PaidanRegisterTabID);
            if (null != modeBackRecoveryDate)
            {
                bUnModify = true;

                if (modeBackRecoveryDate.tdConstDateID > 0 && modelHxdNoTabOld.HstddjDate.Value.Year == 1900)
                {
                    if (modeBackRecoveryDate.tdConstDateID == 1)
                    {
                        modelHxdNoTabOld.HstddjDate = modelHxdNoTabOld.LrHsBgdDate;
                    }
                    else if (modeBackRecoveryDate.tdConstDateID == 2)
                    {
                        modelHxdNoTabOld.HstddjDate = modelHxdNoTabOld.SdHxdDate;
                    }
                }
                else
                {
                    modelHxdNoTabOld.HstddjDate = bModify ? modelHxdNoTabFalse.HstddjDate : this.dateTimePickerGetSheetRegDate.Value.Date;
                }

                if (modeBackRecoveryDate.rcdConstDateID > 0 && modelHxdNoTabOld.HSLCDDate.Value.Year == 1900)
                {
                    if (modeBackRecoveryDate.rcdConstDateID == 1)
                    {
                        modelHxdNoTabOld.HSLCDDate = modelHxdNoTabOld.LrHsBgdDate;
                    }
                    else if (modeBackRecoveryDate.rcdConstDateID == 2)
                    {
                        modelHxdNoTabOld.HSLCDDate = modelHxdNoTabOld.SdHxdDate;
                    }
                }
                else
                {
                    modelHxdNoTabOld.HSLCDDate = bModify ? modelHxdNoTabFalse.HSLCDDate : this.dateTimePickerHSLCD.Value.Date;
                }

                if (modeBackRecoveryDate.lldConstDateID > 0 && modelHxdNoTabOld.HSLLDDate.Value.Year == 1900)
                {
                    if (modeBackRecoveryDate.lldConstDateID == 1)
                    {
                        modelHxdNoTabOld.HSLLDDate = modelHxdNoTabOld.LrHsBgdDate;
                    }
                    else if (modeBackRecoveryDate.lldConstDateID == 2)
                    {
                        modelHxdNoTabOld.HSLLDDate = modelHxdNoTabOld.SdHxdDate;
                    }
                }
                else
                {
                    modelHxdNoTabOld.HSLLDDate = bModify ? modelHxdNoTabFalse.HSLLDDate : this.dateTimePickerHSLLD.Value.Date;
                }

                if (modeBackRecoveryDate.tydConstDateID > 0 && modelHxdNoTabOld.HSTYDDate.Value.Year == 1900)
                {
                    if (modeBackRecoveryDate.tydConstDateID == 1)
                    {
                        modelHxdNoTabOld.HSTYDDate = modelHxdNoTabOld.LrHsBgdDate;
                    }
                    else if (modeBackRecoveryDate.tydConstDateID == 2)
                    {
                        modelHxdNoTabOld.HSTYDDate = modelHxdNoTabOld.SdHxdDate;
                    }
                }
                else
                {
                    modelHxdNoTabOld.HSTYDDate = bModify ? modelHxdNoTabFalse.HSTYDDate : this.dateTimePickerHSTYD.Value.Date;
                }

                if (modeBackRecoveryDate.fxtConstDateID > 0 && modelHxdNoTabOld.HsbdzldjDate.Value.Year == 1900)
                {
                    if (modeBackRecoveryDate.fxtConstDateID == 1)
                    {
                        modelHxdNoTabOld.HsbdzldjDate = modelHxdNoTabOld.LrHsBgdDate;
                    }
                    else if (modeBackRecoveryDate.fxtConstDateID == 2)
                    {
                        modelHxdNoTabOld.HsbdzldjDate = modelHxdNoTabOld.SdHxdDate;
                    }
                }
                else
                {
                    modelHxdNoTabOld.HsbdzldjDate = this.dateTimePickerError.Value.Date;
                }

                if (modeBackRecoveryDate.wtsConstDateID > 0 && modelHxdNoTabOld.QtddjDate.Value.Year == 1900)
                {
                    if (modeBackRecoveryDate.wtsConstDateID == 1)
                    {
                        modelHxdNoTabOld.QtddjDate = modelHxdNoTabOld.LrHsBgdDate;
                    }
                    else if (modeBackRecoveryDate.wtsConstDateID == 2)
                    {
                        modelHxdNoTabOld.QtddjDate = modelHxdNoTabOld.SdHxdDate;
                    }
                }
                else
                {
                    modelHxdNoTabOld.QtddjDate = this.dateTimePickerShortOfSheetRegDate.Value.Date;
                }
            }

            if (!bUnModify)
            {
                modelHxdNoTabOld.HstddjDate = bModify ? modelHxdNoTabFalse.HstddjDate : this.dateTimePickerGetSheetRegDate.Value.Date;
                modelHxdNoTabOld.HSLCDDate = bModify ? modelHxdNoTabFalse.HSLCDDate : this.dateTimePickerHSLCD.Value.Date;
                modelHxdNoTabOld.HSLLDDate = bModify ? modelHxdNoTabFalse.HSLLDDate : this.dateTimePickerHSLLD.Value.Date;
                modelHxdNoTabOld.HSTYDDate = bModify ? modelHxdNoTabFalse.HSTYDDate : this.dateTimePickerHSTYD.Value.Date;
                modelHxdNoTabOld.HsbdzldjDate = this.dateTimePickerError.Value.Date;
                modelHxdNoTabOld.QtddjDate = this.dateTimePickerShortOfSheetRegDate.Value.Date;
            }
        }

        private bool CheckHxdNoTabHSDate(DGBY.Model.HxdNoTab modelHxdNoTabOld, DateTime? dtHstddjDate,
            DateTime? dHSLCDDate, DateTime? dHSLLDDate,DateTime? dHSTYDDate, DateTime? dHsbdzldjDate, DateTime? dQtddjDate)
        {
            //判断回收时间是否允许修改
            if (modelHxdNoTabOld.HstddjDate != dtHstddjDate
                || modelHxdNoTabOld.HSLCDDate != dHSLCDDate
                || modelHxdNoTabOld.HSLLDDate != dHSLLDDate
                || modelHxdNoTabOld.HSTYDDate != dHSTYDDate
                || modelHxdNoTabOld.HsbdzldjDate != dHsbdzldjDate
                || modelHxdNoTabOld.QtddjDate != dQtddjDate)
            {
                DGBY.Model.DG_BackRecoveryMaterialResgistTab modelBackRecoveryMaterialTab = m_bllBackRecoveryMaterialResgistTab.GetModelByPaidanRegisterTabID(Convert.ToInt32(modelHxdNoTabOld.PaidanRegisterTabID));
                if (null == modelBackRecoveryMaterialTab)
                {
                    MessageBox.Show("无该备案资料回收登记表信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }

                if (modelHxdNoTabOld.HstddjDate != dtHstddjDate)
                {
                    if (modelBackRecoveryMaterialTab.td != 4)
                    {
                        MessageBox.Show("该备案资料回收登记表提单不为真，不允许修改!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }

                if (modelHxdNoTabOld.HSLCDDate != dHSLCDDate)
                {
                    if (modelBackRecoveryMaterialTab.rcd != 4)
                    {
                        MessageBox.Show("该备案资料回收登记表入仓单不为真，不允许修改!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }

                if (modelHxdNoTabOld.HSLLDDate != dHSLLDDate)
                {
                    if (modelBackRecoveryMaterialTab.lld != 4)
                    {
                        MessageBox.Show("该备案资料回收登记表六联单不为真，不允许修改!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }

                if (modelHxdNoTabOld.HSTYDDate != dHSTYDDate)
                {
                    if (modelBackRecoveryMaterialTab.tyd != 4)
                    {
                        MessageBox.Show("该备案资料回收登记表托运单不为真，不允许修改!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }

                if (modelHxdNoTabOld.HsbdzldjDate != dHsbdzldjDate)
                {
                    if (modelBackRecoveryMaterialTab.fxt != 4)
                    {
                        MessageBox.Show("该备案资料回收登记表放行条不为真，不允许修改!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }

                if (modelHxdNoTabOld.QtddjDate != dQtddjDate)
                {
                    if (modelBackRecoveryMaterialTab.wts != 4)
                    {
                        MessageBox.Show("该备案资料回收登记表委托书不为真，不允许修改!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
            }

            return true;
        }

        private void ModifySjShouldPayMonth(int nPaidanRegisterTabID, DateTime? dtTongJDate)
        {
            DataSet dsSjList = m_bllFeeSJTruePayTab.GetList("FeeSJTruePayTab.PaidanRegisterTabID ='" + nPaidanRegisterTabID + "'");
            if (dsSjList.Tables.Count > 0 && dsSjList.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dsSjList.Tables[0].Rows.Count; i++)
                {
                    int nFeeSJTruePayTabID = Convert.ToInt32(dsSjList.Tables[0].Rows[i]["FeeSJTruePayTabID"].ToString());

                    DGBY.Model.FeeSJTruePayTab modelSJTruePayTabOld = m_bllFeeSJTruePayTab.GetModel(nFeeSJTruePayTabID);
                    if (modelSJTruePayTabOld.ShouldPayMonth != 0)
                        continue;

                    modelSJTruePayTabOld.ShouldPayMonth = dtTongJDate.Value.Date.Year * 100 + dtTongJDate.Value.Date.Month;

                    m_bllFeeSJTruePayTab.Update(modelSJTruePayTabOld);
                    Frm_Main.Log2DB(this.Text, "修改商检应收费用计提年月", nFeeSJTruePayTabID.ToString());
                }
            }
        }

        private void SetCurrentUGR(DGBY.Model.HxdNoTab modelHxdNoTabOld, string strContractNo)
        {
            UltraGridRow CurrentUGR;
            CurrentUGR = ultraGrid1.Rows[ultraGrid1.ActiveRow.Index];
            DataSet ds = m_bllHxdNoTab.ShowZhDjbRec("PaidanRegisterTab.PaidanRegisterTabID = '" + modelHxdNoTabOld.PaidanRegisterTabID.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                CurrentUGR.Cells["工厂名称"].Value = ds.Tables[0].Rows[0]["工厂名称"].ToString();
                if (ds.Tables[0].Rows[0]["美元"].ToString() != "")
                {
                    CurrentUGR.Cells["美元"].Value = (Decimal)ds.Tables[0].Rows[0]["美元"];
                }
                if (ds.Tables[0].Rows[0]["港币"].ToString() != "")
                {
                    CurrentUGR.Cells["港币"].Value = (Decimal)ds.Tables[0].Rows[0]["港币"];
                }
                if (ds.Tables[0].Rows[0]["美元折扣"].ToString() != "")
                {
                    CurrentUGR.Cells["美元折扣"].Value = (Decimal)ds.Tables[0].Rows[0]["美元折扣"];
                }
                if (ds.Tables[0].Rows[0]["港币折扣"].ToString() != "")
                {
                    CurrentUGR.Cells["港币折扣"].Value = (Decimal)ds.Tables[0].Rows[0]["港币折扣"];
                }
                //CurrentUGR.Cells["港币"].Value = ds.Tables[0].Rows[0]["港币"].ToString() == "" ? 0 : (Decimal)ds.Tables[0].Rows[0]["港币"];
                //CurrentUGR.Cells["美元折扣"].Value = ds.Tables[0].Rows[0]["美元折扣"].ToString() == "" ? 0 : (Decimal)ds.Tables[0].Rows[0]["美元折扣"];
                //CurrentUGR.Cells["港币折扣"].Value = ds.Tables[0].Rows[0]["港币折扣"].ToString() == "" ? 0 : (Decimal)ds.Tables[0].Rows[0]["港币折扣"];
                
                CurrentUGR.Cells["业务员"].Value = ds.Tables[0].Rows[0]["业务员"].ToString();
                CurrentUGR.Cells["集装箱柜号"].Value = ConvertGuiNoString(ds.Tables[0].Rows[0]["集装箱柜号"].ToString());
                CurrentUGR.Cells["部门"].Value = ds.Tables[0].Rows[0]["部门"].ToString();
                CurrentUGR.Cells["目的国家"].Value = ds.Tables[0].Rows[0]["目的国家"].ToString();
                CurrentUGR.Cells["财务人员"].Value = ds.Tables[0].Rows[0]["财务人员"].ToString();
                
            }

            CurrentUGR.Cells["合同编号"].Value = strContractNo;
            if (modelHxdNoTabOld.SBDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["申报日期"].Value = modelHxdNoTabOld.SBDate.Value.Date;
            }
            if (modelHxdNoTabOld.SBDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["申报年月"].Value = modelHxdNoTabOld.SBDate.Value.Date.Year.ToString() + modelHxdNoTabOld.SBDate.Value.Date.Month.ToString().PadLeft(2, '0');
            }

            CurrentUGR.Cells["报关单号"].Value = modelHxdNoTabOld.BGDNo;
            CurrentUGR.Cells["核销单号"].Value = modelHxdNoTabOld.HxdNo;
      

            if (modelHxdNoTabOld.TongJDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["统计日期"].Value = modelHxdNoTabOld.TongJDate.Value.Date;
            }


            if (modelHxdNoTabOld.LrHsBgdDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["录入报关单日期"].Value = modelHxdNoTabOld.LrHsBgdDate.Value.Date;
            }

            if (modelHxdNoTabOld.SdHxdDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["收到核销单日期"].Value = modelHxdNoTabOld.SdHxdDate.Value.Date;
            }


            if (modelHxdNoTabOld.CKouDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["出口日期"].Value = modelHxdNoTabOld.CKouDate.Value.Date;
            }

            if (modelHxdNoTabOld.YhxDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["已核销日期"].Value = modelHxdNoTabOld.YhxDate.Value.Date;
            }

            if (modelHxdNoTabOld.HsGHHTDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["回收购货合同日期"].Value = modelHxdNoTabOld.HsGHHTDate.Value.Date;
            }

            if (modelHxdNoTabOld.JcwTszldDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["交财务退税资料单日期"].Value = modelHxdNoTabOld.JcwTszldDate.Value.Date;
            }

            if (modelHxdNoTabOld.TuiShuiZHDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["退税最后日期"].Value = modelHxdNoTabOld.TuiShuiZHDate.Value.Date;
            }

            if (modelHxdNoTabOld.QtddjDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["委托书回收日期"].Value = modelHxdNoTabOld.QtddjDate.Value.Date;
            }


            if (modelHxdNoTabOld.HSLLDDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["六联单回收日期"].Value = modelHxdNoTabOld.HSLLDDate.Value.Date;
            }

            if (modelHxdNoTabOld.HSTYDDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["托运单回收日期"].Value = modelHxdNoTabOld.HSTYDDate.Value.Date;
            }

            if (modelHxdNoTabOld.HSLCDDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["入仓单回收日期"].Value = modelHxdNoTabOld.HSLCDDate.Value.Date;
            }


            if (modelHxdNoTabOld.HsbdzldjDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["回收放行条登记日期"].Value = modelHxdNoTabOld.HsbdzldjDate.Value.Date;
            }

            if (modelHxdNoTabOld.HstddjDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["回收提单登记日期"].Value = modelHxdNoTabOld.HstddjDate.Value.Date;
            }

            if (modelHxdNoTabOld.SCKHTdjDate.Value.Date.Year != 1900)
            {
                CurrentUGR.Cells["收出口合同登记日期"].Value = modelHxdNoTabOld.SCKHTdjDate.Value.Date;
            }
            //CurrentUGR.Cells["委托书"].Value = modelHxdNoTabOld.WeiTuoBook;

            CurrentUGR.Cells["是否委托书"].Value = Convert.ToString(modelHxdNoTabOld.iswts ? 1 : 0);
            CurrentUGR.Cells["是否六联单"].Value = Convert.ToString(modelHxdNoTabOld.islld ? 1 : 0);
            CurrentUGR.Cells["是否托运单"].Value = Convert.ToString(modelHxdNoTabOld.istyd ? 1 : 0);
            CurrentUGR.Cells["是否入仓单"].Value = Convert.ToString(modelHxdNoTabOld.isrcd ? 1 : 0);
            CurrentUGR.Cells["是否放行条"].Value = Convert.ToString(modelHxdNoTabOld.isfxt ? 1 : 0);
            CurrentUGR.Cells["是否提单"].Value = Convert.ToString(modelHxdNoTabOld.istd ? 1 : 0);
            CurrentUGR.Cells["是否出口合同"].Value = Convert.ToString(modelHxdNoTabOld.isckht ? 1 : 0);

            CurrentUGR.Cells["备注"].Value = modelHxdNoTabOld.Note;
        }

        private bool IsSJ(int nPaidanRegisterTabID)
        {
            bool bRet = false;
            DataSet ds = m_bllFeeSJTruePayTab.GetListInfo("FeeSJTruePayTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID + "' and FactoryDataTabID != 6");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                bRet = true;

            }
            return bRet;
        }
        private void ProcessBGFee(int nPaidanRegisterTabID, string strContractNo)
        {
        
            int nIsSync = 0;
            int nFeeBGTruePayTabID = 0;
            DataSet ds = m_bllFeeBGTruePayTab.GetList("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'", "1=0");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                nIsSync = (int)ds.Tables[0].Rows[0]["是否已同步费用"];
                nFeeBGTruePayTabID = (int)ds.Tables[0].Rows[0]["FeeID"];
            }

            //int nIsRecvBGSync = 0;
            //int nFacFeeTabNewID = 0;
            //ds = m_bllFacFeeTabNew.GetList("FacFeeTabNew.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'");
            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    nIsRecvBGSync = (int)ds.Tables[0].Rows[0]["IsBGFeeSync"];
            //    nFacFeeTabNewID = (int)ds.Tables[0].Rows[0]["FacFeeTabNewID"];
            //}

            DataSet dsTemp = m_bllBGCKHTTab.ShowAllCKHTMX(strContractNo);
            int nItemCount = dsTemp.Tables[0].Rows.Count;
            DataSet dsInfo = m_bllBGCKHTTab.ShowContractInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'");
     
            if (dsInfo.Tables.Count > 0 && dsInfo.Tables[0].Rows.Count > 0)
            {
                int nGuiCount = (int)dsInfo.Tables[0].Rows[0]["柜数"];
                if (nFeeBGTruePayTabID != 0 && nIsSync == 0)
                {
                    bool bRet = IsSJ(nPaidanRegisterTabID);
                    DGBY.Model.FeeBGTruePayTab modelFeeBGTruePayTabOld
   = m_bllFeeBGTruePayTab.GetModel(nFeeBGTruePayTabID);

                    modelFeeBGTruePayTabOld.BGFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付报关费定价"];
                    modelFeeBGTruePayTabOld.LGFee = ((Decimal)dsInfo.Tables[0].Rows[0]["应付连柜费定价"]) * (nGuiCount - 1);
                    if (bRet)
                    {
                        modelFeeBGTruePayTabOld.TGDFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付通关单费定价"];
                    }
               
                    modelFeeBGTruePayTabOld.JYFee = ((Decimal)dsInfo.Tables[0].Rows[0]["应付加页费定价"]) * ((nItemCount - 1)/ 5);

                    modelFeeBGTruePayTabOld.GCFee = 0;
                    modelFeeBGTruePayTabOld.CGFee = 0;
                    modelFeeBGTruePayTabOld.SMFee = 0;
                    //modelFeeBGTruePayTabOld.GCFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付改船费定价"];
                    //modelFeeBGTruePayTabOld.CGFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付查柜费定价"];
                    //modelFeeBGTruePayTabOld.SMFee = (Decimal)dsInfo.Tables[0].Rows[0]["应付扫描费定价"];


                    modelFeeBGTruePayTabOld.BGBGFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收报关费定价"];
                    modelFeeBGTruePayTabOld.BGLGFee = ((Decimal)dsInfo.Tables[0].Rows[0]["应收连柜费定价"]) * (nGuiCount - 1);
                    if (bRet)
                    {
                        modelFeeBGTruePayTabOld.BGTGDFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收通关单费定价"];
                    }

                    modelFeeBGTruePayTabOld.BGGKJSFee = 0;
                    modelFeeBGTruePayTabOld.GKJSFee = 0;
                    //for (int i = 0; i < dsInfo.Tables[0].Rows.Count; ++i)
                    for (int i = 0; i < nGuiCount; ++i)
                    {
                        modelFeeBGTruePayTabOld.BGGKJSFee += (Decimal)dsInfo.Tables[0].Rows[i]["应收港口建设费定价"];
                        modelFeeBGTruePayTabOld.GKJSFee += (Decimal)dsInfo.Tables[0].Rows[i]["应付港口建设费定价"];
                    }

                    modelFeeBGTruePayTabOld.BGJYFee = ((Decimal)dsInfo.Tables[0].Rows[0]["应收加页费定价"]) * ((nItemCount - 1) / 5);


                    //modelFeeBGTruePayTabOld.BGCGFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收查柜费定价"];
                    //modelFeeBGTruePayTabOld.BGGCFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收改船费定价"];
                    //modelFeeBGTruePayTabOld.BGSMFee = (Decimal)dsInfo.Tables[0].Rows[0]["应收扫描费定价"];
                    modelFeeBGTruePayTabOld.BGCGFee = 0;
                    modelFeeBGTruePayTabOld.BGGCFee = 0;
                    modelFeeBGTruePayTabOld.BGSMFee = 0;

                    modelFeeBGTruePayTabOld.IsSync = 1;
                    m_bllFeeBGTruePayTab.Update(modelFeeBGTruePayTabOld);
                }
                
            }


   //         if (dsInfo.Tables.Count > 0 && dsInfo.Tables[0].Rows.Count > 0)
   //         {
   //             if (nFacFeeTabNewID != 0 && nIsRecvBGSync == 0)
   //             {
   //                 DGBY.Model.FacFeeTabNew modelFacFeeTabNewOld
   //= m_bllFacFeeTabNew.GetModel(nFacFeeTabNewID);

                    

   //                 modelFacFeeTabNewOld.IsBGFeeSync = 1;
   //                 m_bllFacFeeTabNew.Update(modelFacFeeTabNewOld);
   //             }

   //         }
           
        }

        private void UpdateShenPiaoDateOnHxdTab(int nPaidanRegisterTabID)
        {
            DataSet ds2
                    = m_bllFinCheckInvoiceRecordDetailTab_New.GetFinInvoiceRecordTabInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID + "'");

            if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
            {
                Decimal dtJiaShuiTotal = 0;
                Decimal dtContractJinE = 0;
                DateTime datetimeTemp = DateTime.Parse("1900-01-01");

                for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                {
                    int nID = (int)ds2.Tables[0].Rows[i]["FinCheckInvoiceRecordDetailTab_NewID"];
                    DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFIRTab_New = m_bllFinCheckInvoiceRecordDetailTab_New.GetModel(nID);
                    dtContractJinE = (Decimal)ds2.Tables[0].Rows[i]["报关金额"];
                    dtJiaShuiTotal += (Decimal)ds2.Tables[0].Rows[i]["开票USD设17"]
                        + (Decimal)ds2.Tables[0].Rows[i]["开票USD设16"]
                        + (Decimal)ds2.Tables[0].Rows[i]["开票USD设15"]
                        + (Decimal)ds2.Tables[0].Rows[i]["开票USD设13"]
                        + (Decimal)ds2.Tables[0].Rows[i]["开票USD设9"]
                        + (Decimal)ds2.Tables[0].Rows[i]["开票USD设5"];
                    DateTime dtTemp = (DateTime)ds2.Tables[0].Rows[i]["审票日期"];
                    if (dtTemp > datetimeTemp)
                        datetimeTemp = dtTemp;

                }

                bool bIsKaiPiaoFinish = false;
                if (dtContractJinE - dtJiaShuiTotal > 0.1M
                    || dtJiaShuiTotal - dtContractJinE > 0.1M)
                {
                    bIsKaiPiaoFinish = false;
                }
                else
                {
                    bIsKaiPiaoFinish = true;
                }

                if (bIsKaiPiaoFinish)
                {
                    DataSet ds = m_bllHxdNoTab.GetList("PaidanRegisterTabID = '" + nPaidanRegisterTabID + "'");

                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        int nHxdNoTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["HxdNoTabID"].ToString(), 10);
                        DGBY.Model.HxdNoTab modelHxdNoTab = m_bllHxdNoTab.GetModel(nHxdNoTabID);
                        modelHxdNoTab.CheckInvoiceDate = datetimeTemp;
                        m_bllHxdNoTab.Update(modelHxdNoTab);
                    }
                }
            }
        }
        private void ShowAllRecordInDataGridViewEx(string strWhere)
        {
            string strTemp = this.comboBoxShowWay.Text;
            if (strTemp == "全部记录")
                ShowAllRecord(strWhere);
            else
            {
                DateTime dtNow = System.DateTime.Now.Date.AddDays(-180);
                ShowAllRecord("(PaidanRegisterTab.PaidanRegisterTabID = 127 or year(HxdNoTab.SBDate) = 1900 or (case when year(HxdNoTab.SBDate) = 1900 then null else HxdNoTab.SBDate end) >= '" + dtNow + "')" + strWhere);
            }
        }
        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            m_dicSelectID2RowNo.Clear();
            ShowAllRecordInDataGridViewEx("");
           // ShowAllRecord("PaidanRegisterTab.ContractCode is not null");
        }


        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }

       
        private bool IsNum(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[1-9]+\d*$");
        }

        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }

      
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    //DGBY.Model.HxdNoTab modelHxdNoTabOld = new DGBY.Model.HxdNoTab();

            //    int nPaidanRegisterTabID = 0;
            //    DataSet ds = m_bllPaidanRegisterTab.GetList("ContractCode = '" + this.textBoxContractNo.Text + "'");
            //    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //    {
            //        nPaidanRegisterTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
            //    }
            //    //DateTime dtSBDate = new DateTime();
            //    //ds = m_bllBGDComeBackTab.ShowOneBGDComeBackkRec("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID + "'");
            //    //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //    //{
            //    //    dtSBDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["申报日期"].ToString());
            //    //}
            //    long lHxdNo = Convert.ToInt64(this.textBoxHXDNo.Text, 10);
            //    int nJCHxdDjTabId = 0;
            //    //ds = m_bllJCHxdDjTab.GetPostRegListEx(lHxdNo.ToString() + " BETWEEN QsHxdNo and  ZzHxdNo");
            //    //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //    //{
            //    //    nJCHxdDjTabId = Convert.ToInt32(ds.Tables[0].Rows[0]["JCHxdDjTabID"].ToString(), 10);
            //    //}
            //    //else
            //    //{
            //    //    MessageBox.Show("核销单号不合法!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    //    return;
            //    //}
            //    DGBY.Model.HxdNoTab modelHxdNoTab = new DGBY.Model.HxdNoTab();
            //    modelHxdNoTab.PaidanRegisterTabID = nPaidanRegisterTabID;
            //    modelHxdNoTab.JCHxdDjTabID = nJCHxdDjTabId;
            //    modelHxdNoTab.HxdNo = Convert.ToInt64(this.textBoxHXDNo.Text, 10);
            //    modelHxdNoTab.Note = this.textBoxNote.Text;
            //    //modelHxdNoTab.HxdNoTabID = modelHxdNoTabOld.HxdNoTabID;

            //    //modelHxdNoTab.TuiShuiZHDate = this.dateTimePickerInputDate.Value;//有问题

            //    modelHxdNoTab.TongJDate = this.dateTimePickerTongJi.Value;
            //    modelHxdNoTab.CKouDate = this.dateTimePicker1CustomerDate.Value;
            //    modelHxdNoTab.BGDNo = this.textBoxBGSheetNo.Text;
            //    modelHxdNoTab.SBDate = this.dateTimePickerDeclareDate.Value;
            //    modelHxdNoTab.LrHsBgdDate = this.dateTimePickerInputDate.Value;
            //    modelHxdNoTab.SdHxdDate = this.dateTimePickerRecHXDDate.Value;
            //    modelHxdNoTab.YhxDate = this.dateTimePickerAlreadyHXDate.Value;
            //    modelHxdNoTab.HZzsInvDate = DateTime.Parse("1900-01-01");
            //    modelHxdNoTab.JcGHHTDate = DateTime.Parse("1900-01-01");
            //    modelHxdNoTab.HsGHHTDate = this.dateTimePickerRecBuyGoodsContractDate.Value;
            //    modelHxdNoTab.JcwTszldDate = this.dateTimePickerTaxBackInfoDate.Value;
            //    modelHxdNoTab.QtddjDate = this.dateTimePickerShortOfSheetRegDate.Value;
            //    modelHxdNoTab.HstddjDate = this.dateTimePickerGetSheetRegDate.Value;
            //    modelHxdNoTab.SCKHTdjDate = this.dateTimePickerGetCustomerContractRegDate.Value;
            //    modelHxdNoTab.XbdzldjDate = DateTime.Parse("1900-01-01");
            //    modelHxdNoTab.HsbdzldjDate = DateTime.Parse("1900-01-01");
            //    modelHxdNoTab.ZlccdjDate = DateTime.Parse("1900-01-01");
            //    modelHxdNoTab.HsggzldjDate = DateTime.Parse("1900-01-01");
            //    modelHxdNoTab.TuiShuiZHDate = this.dateTimePickerDeclareDate.Value.AddDays(80);


            //    m_bllHxdNoTab.Add(modelHxdNoTab);
            //    //ShowAllRecordInDataGridView();
            //    ShowAllRecord("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'");

            //    //int nBGDComeBackTabID = 0;
            //    //ds = m_bllBGDComeBackTab.ShowOneBGDComeBackkRec("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID + "'");
            //    //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //    //{
            //    //    nBGDComeBackTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BGDComeBackTabID"].ToString(), 10);
            //    //    DGBY.Model.BGDComeBackTab modelBGDComeBackTabOld = m_bllBGDComeBackTab.GetModel(nBGDComeBackTabID);
            //    //    modelBGDComeBackTabOld.SBDate = modelHxdNoTab.SBDate;
            //    //    modelBGDComeBackTabOld.CKouDate = modelHxdNoTab.CKouDate;
            //    //    modelBGDComeBackTabOld.TongJDate = modelHxdNoTab.TongJDate;
            //    //    modelBGDComeBackTabOld.BGDNo = modelHxdNoTab.BGDNo;
            //    //    modelBGDComeBackTabOld.PZWH = modelHxdNoTab.HxdNo.ToString();
            //    //    m_bllBGDComeBackTab.Update(modelBGDComeBackTabOld);
            //    //}

            //}
            //catch (System.Exception ex)
            //{
            //    MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //}

        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(m_nSelectHxdNoTabID);
                if (modelHxdNoTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                //int nPaidanRegisterTabID = 0;
                //DataSet ds = m_bllPaidanRegisterTab.GetList("ContractCode = '" + this.textBoxContractNo.Text + "'");
                //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //{
                //    nPaidanRegisterTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
                //}

                m_bllHxdNoTab.Delete(m_nSelectHxdNoTabID);
                ShowAllRecordInDataGridViewEx(" and HxdNoTab.HxdNoTabID = '" + m_nSelectHxdNoTabID.ToString() + "'");
                MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private void dateTimePickerInputDate_ValueChanged(object sender, EventArgs e)
        {

        }

        private void rowMergeView1_BeforeRowFilterChanged(object sender, BeforeRowFilterChangedEventArgs e)
        {
            
         //   string str = e.NewColumnFilter.LogicalOperator.ToString();
            //if (e.NewColumnFilter.Column.DataType == typeof(DateTime) 
            //    && e.NewColumnFilter.Column.FilterOperatorDefaultValue == FilterOperatorDefaultValue.Default)
            //{
            //    //if (rowMergeView1.ActiveCell.Value == null)
            //    //    return;
  
            //    //DateTime dtTemp = (DateTime)rowMergeView1.ActiveCell.Value;
            //    //ColumnFilter colFilter = this.rowMergeView1.DisplayLayout.Bands[0].ColumnFilters[e.NewColumnFilter.Column.Header.Caption];
            //    //UltraGridColumn column = this.rowMergeView1.DisplayLayout.Bands[0].Columns[e.NewColumnFilter.Column.Header.Caption];
            //    //FilterCondition originalFilterCondition = new FilterCondition(column, FilterComparisionOperator.Custom, dtTemp);
            //    //e.NewColumnFilter.FilterConditions.Clear();
            //    //e.NewColumnFilter.FilterConditions.Add(new DateFilterCondition(originalFilterCondition));
               
            //}
        }

        private void buttonBatchModify_Click(object sender, EventArgs e)
        {
            if (m_activeCell == null)
            {
                MessageBox.Show("请选择要批量修改的列", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
           
  
            //Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
            //ugr[0].Appearance.BackColor = Color.Gray;
            if (m_dicSelectID2RowNo.Count <= 0)
            {
                MessageBox.Show("没有要批量修改的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //bool bAllModify = true;
           
            //for (int i = 0; i < ugr.Length; i++)
            //{
            //    if (ugr[i].Appearance.BackColor == Color.Gray)
            //    {
            //        bAllModify = false;
            //        break;
            //    }
                
            //}
            //if (bAllModify)
            {
                DialogResult dr = MessageBox.Show("批量修改" + labelCurrMod.Text + ",本次批量修改会影响到" + m_dicSelectID2RowNo.Count.ToString() + "条记录，确实要批量修改么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }
            }
            string strColumnName = m_activeCell.Column.Header.Caption;
            if (strColumnName == "统计日期")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);
                    modelHxdNoTabOld.TongJDate = this.dateTimePickerTongJi.Value.Date;
                    m_bllHxdNoTab.Update(modelHxdNoTabOld);
           
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = this.dateTimePickerTongJi.Value.Date;
                }
            }

            else if (strColumnName == "录入报关单日期")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);
                    modelHxdNoTabOld.LrHsBgdDate = this.dateTimePickerInputDate.Value.Date;
                    m_bllHxdNoTab.Update(modelHxdNoTabOld);
        
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = this.dateTimePickerInputDate.Value.Date;
                    //
                }
            }
            else if (strColumnName == "回收购货合同日期")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);
                    modelHxdNoTabOld.HsGHHTDate = this.dateTimePickerRecBuyGoodsContractDate.Value.Date;
                    m_bllHxdNoTab.Update(modelHxdNoTabOld);
                    
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = this.dateTimePickerRecBuyGoodsContractDate.Value.Date;
                    
                }
            }

            else if (strColumnName == "交财务退税资料单日期")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);
                    modelHxdNoTabOld.JcwTszldDate = this.dateTimePickerTaxBackInfoDate.Value.Date;
                    m_bllHxdNoTab.Update(modelHxdNoTabOld);
                    
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = this.dateTimePickerTaxBackInfoDate.Value.Date;
                    
                }
            }
            else if (strColumnName == "委托书回收日期")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);

                    //获取修改前回收时间
                    DateTime? dtHstddjDate = modelHxdNoTabOld.HstddjDate;
                    DateTime? dHSLCDDate = modelHxdNoTabOld.HSLCDDate;
                    DateTime? dHSLLDDate = modelHxdNoTabOld.HSLLDDate;
                    DateTime? dHSTYDDate = modelHxdNoTabOld.HSTYDDate;
                    DateTime? dHsbdzldjDate = modelHxdNoTabOld.HsbdzldjDate;
                    DateTime? dQtddjDate = modelHxdNoTabOld.QtddjDate;
                    DateTime? dSCKHTdjDate = modelHxdNoTabOld.SCKHTdjDate;

                    bool bModify = false;
                    DGBY.Model.DG_BackRecoveryDateCorrespondTab modeBackRecoveryDate = GetBackRecoveryDateModelByPaidanID((int)modelHxdNoTabOld.PaidanRegisterTabID);
                    if (null != modeBackRecoveryDate)
                    {
                        if (modeBackRecoveryDate.wtsConstDateID > 0 && modelHxdNoTabOld.QtddjDate.Value.Year == 1900)
                        {
                            if (modeBackRecoveryDate.wtsConstDateID == 1)
                                modelHxdNoTabOld.QtddjDate = modelHxdNoTabOld.LrHsBgdDate;
                            else if (modeBackRecoveryDate.wtsConstDateID == 2)
                                modelHxdNoTabOld.QtddjDate = modelHxdNoTabOld.SdHxdDate;

                            bModify = true;
                        }
                    }

                    if (!bModify)
                        modelHxdNoTabOld.QtddjDate = this.dateTimePickerShortOfSheetRegDate.Value.Date;

                    m_bllHxdNoTab.Update(modelHxdNoTabOld);

                    UpdateBackRecoveryMaterialTab(modelHxdNoTabOld, dtHstddjDate, dHSLCDDate, dHSLLDDate, dHSTYDDate, dHsbdzldjDate, dQtddjDate, dSCKHTdjDate);

                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = modelHxdNoTabOld.QtddjDate;   
                }
            }
            else if (strColumnName == "回收放行条登记日期")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);

                    //获取修改前回收时间
                    DateTime? dtHstddjDate = modelHxdNoTabOld.HstddjDate;
                    DateTime? dHSLCDDate = modelHxdNoTabOld.HSLCDDate;
                    DateTime? dHSLLDDate = modelHxdNoTabOld.HSLLDDate;
                    DateTime? dHSTYDDate = modelHxdNoTabOld.HSTYDDate;
                    DateTime? dHsbdzldjDate = modelHxdNoTabOld.HsbdzldjDate;
                    DateTime? dQtddjDate = modelHxdNoTabOld.QtddjDate;
                    DateTime? dSCKHTdjDate = modelHxdNoTabOld.SCKHTdjDate;

                    bool bModify = false;
                    DGBY.Model.DG_BackRecoveryDateCorrespondTab modeBackRecoveryDate = GetBackRecoveryDateModelByPaidanID((int)modelHxdNoTabOld.PaidanRegisterTabID);
                    if (null != modeBackRecoveryDate)
                    {
                        if (modeBackRecoveryDate.fxtConstDateID > 0 && modelHxdNoTabOld.HsbdzldjDate.Value.Year == 1900)
                        {
                            if (modeBackRecoveryDate.fxtConstDateID == 1)
                            {
                                modelHxdNoTabOld.HsbdzldjDate = modelHxdNoTabOld.LrHsBgdDate;
                            }
                            else if (modeBackRecoveryDate.fxtConstDateID == 2)
                            {
                                modelHxdNoTabOld.HsbdzldjDate = modelHxdNoTabOld.SdHxdDate;
                            }

                            bModify = true;
                        }
                    }

                    if (!bModify)
                        modelHxdNoTabOld.HsbdzldjDate = this.dateTimePickerError.Value.Date;
                    //modelHxdNoTabOld.QtddjDate = this.dateTimePickerError.Value.Date;
                    m_bllHxdNoTab.Update(modelHxdNoTabOld);

                    UpdateBackRecoveryMaterialTab(modelHxdNoTabOld, dtHstddjDate, dHSLCDDate, dHSLLDDate, dHSTYDDate, dHsbdzldjDate, dQtddjDate, dSCKHTdjDate);
                    
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = modelHxdNoTabOld.HsbdzldjDate;
                    
                }
            }
            else if (strColumnName == "回收提单登记日期")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);

                    //获取修改前回收时间
                    DateTime? dtHstddjDate = modelHxdNoTabOld.HstddjDate;
                    DateTime? dHSLCDDate = modelHxdNoTabOld.HSLCDDate;
                    DateTime? dHSLLDDate = modelHxdNoTabOld.HSLLDDate;
                    DateTime? dHSTYDDate = modelHxdNoTabOld.HSTYDDate;
                    DateTime? dHsbdzldjDate = modelHxdNoTabOld.HsbdzldjDate;
                    DateTime? dQtddjDate = modelHxdNoTabOld.QtddjDate;
                    DateTime? dSCKHTdjDate = modelHxdNoTabOld.SCKHTdjDate;

                    bool bModify = false;
                    DGBY.Model.DG_BackRecoveryDateCorrespondTab modeBackRecoveryDate = GetBackRecoveryDateModelByPaidanID((int)modelHxdNoTabOld.PaidanRegisterTabID);
                    if (null != modeBackRecoveryDate)
                    {
                        if (modeBackRecoveryDate.tdConstDateID > 0 && modelHxdNoTabOld.HstddjDate.Value.Year == 1900)
                        {
                            if (modeBackRecoveryDate.tdConstDateID == 1)
                            {
                                modelHxdNoTabOld.HstddjDate = modelHxdNoTabOld.LrHsBgdDate;
                            }
                            else if (modeBackRecoveryDate.tdConstDateID == 2)
                            {
                                modelHxdNoTabOld.HstddjDate = modelHxdNoTabOld.SdHxdDate;
                            }

                            bModify = true;
                        }
                    }

                    if (!bModify)
                        modelHxdNoTabOld.HstddjDate = this.dateTimePickerGetSheetRegDate.Value.Date;

                    m_bllHxdNoTab.Update(modelHxdNoTabOld);

                    UpdateBackRecoveryMaterialTab(modelHxdNoTabOld, dtHstddjDate, dHSLCDDate, dHSLLDDate, dHSTYDDate, dHsbdzldjDate, dQtddjDate, dSCKHTdjDate);
             
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = modelHxdNoTabOld.HstddjDate;
                    
                }
            }
            else if (strColumnName == "收出口合同登记日期")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);
                    modelHxdNoTabOld.SCKHTdjDate = this.dateTimePickerGetCustomerContractRegDate.Value.Date;
                    m_bllHxdNoTab.Update(modelHxdNoTabOld);
                    
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = this.dateTimePickerGetCustomerContractRegDate.Value.Date;
                    
                }
            }


            else if (strColumnName == "入仓单回收日期")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);

                    //获取修改前回收时间
                    DateTime? dtHstddjDate = modelHxdNoTabOld.HstddjDate;
                    DateTime? dHSLCDDate = modelHxdNoTabOld.HSLCDDate;
                    DateTime? dHSLLDDate = modelHxdNoTabOld.HSLLDDate;
                    DateTime? dHSTYDDate = modelHxdNoTabOld.HSTYDDate;
                    DateTime? dHsbdzldjDate = modelHxdNoTabOld.HsbdzldjDate;
                    DateTime? dQtddjDate = modelHxdNoTabOld.QtddjDate;
                    DateTime? dSCKHTdjDate = modelHxdNoTabOld.SCKHTdjDate;

                    bool bModify = false;
                    DGBY.Model.DG_BackRecoveryDateCorrespondTab modeBackRecoveryDate = GetBackRecoveryDateModelByPaidanID((int)modelHxdNoTabOld.PaidanRegisterTabID);
                    if (null != modeBackRecoveryDate)
                    {
                        if (modeBackRecoveryDate.rcdConstDateID > 0 && modelHxdNoTabOld.HSLCDDate.Value.Year == 1900)
                        {
                            if (modeBackRecoveryDate.rcdConstDateID == 1)
                            {
                                modelHxdNoTabOld.HSLCDDate = modelHxdNoTabOld.LrHsBgdDate;
                            }
                            else if (modeBackRecoveryDate.rcdConstDateID == 2)
                            {
                                modelHxdNoTabOld.HSLCDDate = modelHxdNoTabOld.SdHxdDate;
                            }

                            bModify = true;
                        }
                    }

                    if (!bModify)
                        modelHxdNoTabOld.HSLCDDate = this.dateTimePickerHSLCD.Value.Date;

                    m_bllHxdNoTab.Update(modelHxdNoTabOld);

                    UpdateBackRecoveryMaterialTab(modelHxdNoTabOld, dtHstddjDate, dHSLCDDate, dHSLLDDate, dHSTYDDate, dHsbdzldjDate, dQtddjDate, dSCKHTdjDate);

                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = modelHxdNoTabOld.HSLCDDate;
                    
                }
            }
            else if (strColumnName == "托运单回收日期")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);

                    //获取修改前回收时间
                    DateTime? dtHstddjDate = modelHxdNoTabOld.HstddjDate;
                    DateTime? dHSLCDDate = modelHxdNoTabOld.HSLCDDate;
                    DateTime? dHSLLDDate = modelHxdNoTabOld.HSLLDDate;
                    DateTime? dHSTYDDate = modelHxdNoTabOld.HSTYDDate;
                    DateTime? dHsbdzldjDate = modelHxdNoTabOld.HsbdzldjDate;
                    DateTime? dQtddjDate = modelHxdNoTabOld.QtddjDate;
                    DateTime? dSCKHTdjDate = modelHxdNoTabOld.SCKHTdjDate;

                    bool bModify = false;
                    DGBY.Model.DG_BackRecoveryDateCorrespondTab modeBackRecoveryDate = GetBackRecoveryDateModelByPaidanID((int)modelHxdNoTabOld.PaidanRegisterTabID);
                    if (null != modeBackRecoveryDate)
                    {
                        if (modeBackRecoveryDate.tydConstDateID > 0 && modelHxdNoTabOld.HSTYDDate.Value.Year == 1900)
                        {
                            if (modeBackRecoveryDate.tydConstDateID == 1)
                            {
                                modelHxdNoTabOld.HSTYDDate = modelHxdNoTabOld.LrHsBgdDate;
                            }
                            else if (modeBackRecoveryDate.tydConstDateID == 2)
                            {
                                modelHxdNoTabOld.HSTYDDate = modelHxdNoTabOld.SdHxdDate;
                            }

                            bModify = true;
                        }
                    }

                    if (!bModify)
                        modelHxdNoTabOld.HSTYDDate = this.dateTimePickerHSTYD.Value.Date;

                    m_bllHxdNoTab.Update(modelHxdNoTabOld);

                    UpdateBackRecoveryMaterialTab(modelHxdNoTabOld, dtHstddjDate, dHSLCDDate, dHSLLDDate, dHSTYDDate, dHsbdzldjDate, dQtddjDate, dSCKHTdjDate);
                    
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = modelHxdNoTabOld.HSTYDDate;
                    
                }
            }
            else if (strColumnName == "六联单回收日期")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);

                    //获取修改前回收时间
                    DateTime? dtHstddjDate = modelHxdNoTabOld.HstddjDate;
                    DateTime? dHSLCDDate = modelHxdNoTabOld.HSLCDDate;
                    DateTime? dHSLLDDate = modelHxdNoTabOld.HSLLDDate;
                    DateTime? dHSTYDDate = modelHxdNoTabOld.HSTYDDate;
                    DateTime? dHsbdzldjDate = modelHxdNoTabOld.HsbdzldjDate;
                    DateTime? dQtddjDate = modelHxdNoTabOld.QtddjDate;
                    DateTime? dSCKHTdjDate = modelHxdNoTabOld.SCKHTdjDate;

                    bool bModify = false;
                    DGBY.Model.DG_BackRecoveryDateCorrespondTab modeBackRecoveryDate = GetBackRecoveryDateModelByPaidanID((int)modelHxdNoTabOld.PaidanRegisterTabID);
                    if (null != modeBackRecoveryDate)
                    {
                        if (modeBackRecoveryDate.lldConstDateID > 0 && modelHxdNoTabOld.HSLLDDate.Value.Year == 1900)
                        {
                            if (modeBackRecoveryDate.lldConstDateID == 1)
                            {
                                modelHxdNoTabOld.HSLLDDate = modelHxdNoTabOld.LrHsBgdDate;
                            }
                            else if (modeBackRecoveryDate.lldConstDateID == 2)
                            {
                                modelHxdNoTabOld.HSLLDDate = modelHxdNoTabOld.SdHxdDate;
                            }

                            bModify = true;
                        }
                    }

                    if (!bModify)
                        modelHxdNoTabOld.HSLLDDate = this.dateTimePickerHSLLD.Value.Date;

                    m_bllHxdNoTab.Update(modelHxdNoTabOld);

                    UpdateBackRecoveryMaterialTab(modelHxdNoTabOld, dtHstddjDate, dHSLCDDate, dHSLLDDate, dHSTYDDate, dHsbdzldjDate, dQtddjDate, dSCKHTdjDate);
                    
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = modelHxdNoTabOld.HSLLDDate;
                    
                }
            }

            else if (strColumnName == "备注")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);
                    modelHxdNoTabOld.Note = this.textBoxNote.Text;
                    //modelHxdNoTabOld.WeiTuoBook = this.textBoxWeiTuoBook.Text;
                    
                    m_bllHxdNoTab.Update(modelHxdNoTabOld);
                  
                    
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = this.textBoxNote.Text;

                    
                }
            }
            else if (strColumnName == "委托书")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);
                    //modelHxdNoTabOld.Note = this.textBoxNote.Text;
                    modelHxdNoTabOld.WeiTuoBook = this.textBoxWeiTuoBook.Text;

                    m_bllHxdNoTab.Update(modelHxdNoTabOld);

                    
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = this.textBoxWeiTuoBook.Text;

                    
                }
            }
            else if (strColumnName == "已核销日期")
            {
                foreach (int nID in m_dicSelectID2RowNo.Keys)
                {
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nID);
                    modelHxdNoTabOld.YhxDate = this.dateTimePickerAlreadyHXDate.Value.Date;
                    m_bllHxdNoTab.Update(modelHxdNoTabOld);
                    
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                    CurrentUGR.Cells[strColumnName].Value = this.dateTimePickerAlreadyHXDate.Value.Date;
                    
                }
            }
            else
            {
                MessageBox.Show(strColumnName + ",该列不支持批量修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            Frm_Main.Log2DB(this.Text, "批量修改", strColumnName);
            MessageBox.Show("批量修改" + strColumnName + "成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            m_dicSelectID2RowNo.Clear();
        }

        private void rowMergeView1_SummaryValueChanged(object sender, SummaryValueChangedEventArgs e)
        {

        }

        private void rowMergeView1_MouseDown(object sender, MouseEventArgs e)
        {
            UltraGrid grid = (UltraGrid)sender;
            UIElement element = grid.DisplayLayout.UIElement.LastElementEntered;

            Infragistics.Win.UltraWinGrid.ColumnHeader columnHeader = element.GetContext(typeof(Infragistics.Win.UltraWinGrid.ColumnHeader)) as Infragistics.Win.UltraWinGrid.ColumnHeader;
            if (columnHeader != null)
            {
                string str = columnHeader.Caption;
                DataSet ds = m_bllBiZhiTab.GetList("BiZhiName = '" + str + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 || str == "美元折扣" || str == "港币折扣")
                {
                    Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                    if (ugr.Length <= 0)
                    {
                        return;
                    }
                    Decimal dtTemp = 0;
                    for (int i = 0; i < ugr.Length; i++)
                    {
                        string strTemp = ugr[i].Cells[str].Value.ToString();
                        if(strTemp != "")
                            dtTemp += Convert.ToDecimal(ugr[i].Cells[str].Value.ToString());
                    }
                    this.textBoxTotal.Text = dtTemp.ToString();

                }

                //if(bool f=age is int;
             
              
                // Set the grid's tooltip here
               // Debug.WriteLine("ToolTip");
            }
            else
            {
                // Set the grid's tooltip to null. 
              //  Debug.WriteLine("null");
            }  
        }

        private void buttonHxdUnuseNotify60Dayws_Click(object sender, EventArgs e)
        {
            m_bllHxdNoTab.row2col();
            ultraGrid1.DataSource = null;
            
            DataSet ds = m_bllHxdNoTab.ShowZhDjbRec(@"HxdNoTab.Note = '' 
                                                  and Year(HxdNoTab.SBDate) = 1900
                                                  and DATEDIFF(day, JCHxdDjTab.JCDZDate, getdate()-60) > 0");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

                ultraGrid1.DataSource = ds.Tables[0];

                this.ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].CellMultiLine = Infragistics.Win.DefaultableBoolean.True;
                //ConvertStringToMultiLineFirst();

                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[7].Hidden = true;
                ProcessMultiHeader();
            }
        }

        private void buttonHxdUnuseNotify90Dayws_Click(object sender, EventArgs e)
        {
            m_bllHxdNoTab.row2col();
            ultraGrid1.DataSource = null;

            DataSet ds = m_bllHxdNoTab.ShowZhDjbRec(@"HxdNoTab.Note = '' 
                                                  and Year(HxdNoTab.SBDate) = 1900
                                                  and DATEDIFF(day, JCHxdDjTab.JCDZDate, getdate()-90) > 0");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

                ultraGrid1.DataSource = ds.Tables[0];

                this.ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].CellMultiLine = Infragistics.Win.DefaultableBoolean.True;
                //ConvertStringToMultiLineFirst();

                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[7].Hidden = true;
                ProcessMultiHeader();
            }
        }

        private void buttonHxdOverDateNotify45Dayws_Click(object sender, EventArgs e)
        {
            m_bllHxdNoTab.row2col();
            ultraGrid1.DataSource = null;

            DataSet ds = m_bllHxdNoTab.ShowZhDjbRec(@"Year(HxdNoTab.SBDate) > 1900
                                                    and Year(HxdNoTab.SdHxdDate) = 1900
                                                  and DATEDIFF(day, HxdNoTab.SBDate, getdate()-45) > 0");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

                ultraGrid1.DataSource = ds.Tables[0];

                this.ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].CellMultiLine = Infragistics.Win.DefaultableBoolean.True;
                //ConvertStringToMultiLineFirst();

                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[7].Hidden = true;
                ProcessMultiHeader();
            }
        }

        private void buttonHxdOverDateNotify60Dayws_Click(object sender, EventArgs e)
        {
            m_bllHxdNoTab.row2col();
            ultraGrid1.DataSource = null;

            DataSet ds = m_bllHxdNoTab.ShowZhDjbRec(@"Year(HxdNoTab.SBDate) > 1900
                                                    and Year(HxdNoTab.SdHxdDate) = 1900
                                                  and DATEDIFF(day, HxdNoTab.SBDate, getdate()-60) > 0");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

                ultraGrid1.DataSource = ds.Tables[0];

                this.ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].CellMultiLine = Infragistics.Win.DefaultableBoolean.True;
                //ConvertStringToMultiLineFirst();

                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[7].Hidden = true;
                ProcessMultiHeader();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_bllHxdNoTab.row2col();
            ultraGrid1.DataSource = null;

            DataSet ds = m_bllHxdNoTab.ShowZhDjbRec(@"Year(HxdNoTab.CKouDate) > 1900
                                                    and Year(HxdNoTab.JcwTszldDate) = 1900
                                                  and DATEDIFF(day, HxdNoTab.CKouDate, getdate()-70) > 0");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

                ultraGrid1.DataSource = ds.Tables[0];

                this.ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].CellMultiLine = Infragistics.Win.DefaultableBoolean.True;
                //ConvertStringToMultiLineFirst();

                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[7].Hidden = true;
                ProcessMultiHeader();
            }
        }

        private void 从集合中选中ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
            for (int i = 0; i < ugr.Length; i++)
            {
                if (ugr[i].Index == m_nDelIndex)
                    ugr[i].Appearance.BackColor = Color.Gray;
            }
        }

        private void buttonSetHideColumn_Click(object sender, EventArgs e)
        {
            m_lstHideColIndex.Clear();
            Dictionary<string, int> dicColumnName2Index = new Dictionary<string, int>();

            int nIndex = 0;
            foreach (UltraGridColumn column2 in this.ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                if (column2.Hidden == false)
                {
                    dicColumnName2Index.Add(column2.Header.Caption, nIndex);
                }
                nIndex++;
            }

            Frm_HideColumnSet frm_HideColumnSet = new Frm_HideColumnSet(dicColumnName2Index);
            frm_HideColumnSet.StartPosition = FormStartPosition.CenterScreen;
            if (frm_HideColumnSet.ShowDialog() == DialogResult.OK)
            {
                List<int> lstTemp = frm_HideColumnSet.GetHideColumnIndexList();
                foreach (int nIndexTemp in lstTemp)
                {
                    m_lstHideColIndex.Add(nIndexTemp);
                }
            }
            ShowAllRecordInDataGridViewEx("");
        }

        //private void button2_Click(object sender, EventArgs e)
        //{
        //    //ShowAllRecordReLoad("");
        //}

        private void buttonUnbindContract_Click(object sender, EventArgs e)
        {
            DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(m_nSelectHxdNoTabID);
            if (modelHxdNoTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要将核销单号" + modelHxdNoTabOld.HxdNo.ToString() + "解除绑定么？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                modelHxdNoTabOld.PaidanRegisterTabID = 127;
                modelHxdNoTabOld.TongJDate = DateTime.Parse("1900-01-01");
                modelHxdNoTabOld.LrHsBgdDate = DateTime.Parse("1900-01-01");
                modelHxdNoTabOld.HsGHHTDate = DateTime.Parse("1900-01-01");
                modelHxdNoTabOld.SCKHTdjDate = DateTime.Parse("1900-01-01");
                m_bllHxdNoTab.Update(modelHxdNoTabOld);

                Frm_Main.Log2DB(this.Text, "解除绑定", this.textBoxContractNo.Text);
                MessageBox.Show("解除绑定成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                return;
            }
          

        }

        private void ultraGridExcelExporter1_CellExported(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportedEventArgs e)
        {
            Worksheet ws = e.CurrentWorksheet;
            int iRdex = e.CurrentRowIndex;
            int iCdex = e.CurrentColumnIndex;
            //e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].CellFormat.FillPatternForegroundColor = Color.FromArgb(0, 0, 0); 
            // string strColumnName = e.GridColumn;
            if (e.GridColumn.Key == "起始的核销单号" || e.GridColumn.Key == "终止的核销单号")
            {
                //m_ugr[iRdex-1].Cells[e.GridColumn.Key + "公式"].Value.ToString()
                string strNo = e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].Value.ToString();
                int nNo = Convert.ToInt32(strNo, 10);
                if (nNo < 100000000)
                    e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].Value = "0" + e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].Value;
            }
        }

        private void dateTimePicker1CustomerDate_ValueChanged(object sender, EventArgs e)
        {
            if (m_bDateChangeValid)
            {
                DateTime dtNow = System.DateTime.Now.Date;
                this.dateTimePickerRecHXDDate.Value = dtNow;
                DateTime dt = this.dateTimePickerDeclareDate.Value;
                if (dt >= DateTime.Parse("2012-08-01"))
                {
                    this.dateTimePickerAlreadyHXDate.Value = dtNow;
                }
            }
        }

        private void buttonShowAllBG_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridViewEx(" and (HxdNoTab.HxdNo != 0 or (CASE WHEN bghold.BGHNameJX IS NOT NULL THEN bghold.BGHNameJX ELSE bghnew.BGHNameJX END) != '')");

            //ShowAllRecord("BiZhiRow2Col.港币 IS not null or  BiZhiRow2Col.美元 IS not NULL AND PaidanRegisterTab.ContractCode != ''");
        }

        private void buttonShowAllPD_Click(object sender, EventArgs e)
        {
            //ShowAllRecord("BiZhiRow2Col.港币 IS null AND  BiZhiRow2Col.美元 IS NULL AND PaidanRegisterTab.ContractCode != ''");
            ShowAllRecordInDataGridViewEx(" and (HxdNoTab.HxdNo = 0 and (CASE WHEN bghold.BGHNameJX IS NOT NULL THEN bghold.BGHNameJX ELSE bghnew.BGHNameJX END) = '')");
            
        }

        //private void button2_Click_1(object sender, EventArgs e)
        //{
        //    ShowAllRecord("");
        //}

        private void textBoxContractNo_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void ultraGrid1_CellChange(object sender, CellEventArgs e)
        {
            if (!StringComparer.Ordinal.Equals(e.Cell.Column.Key, @"全选"))
            {
                m_bLastModifiedCellIsSelect = false;
                return;
            }
            m_bLastModifiedCellIsSelect = true;
            int iIndex = e.Cell.Row.Index;
            if (iIndex < 0)
                return;
            UltraGridRow CurrentUGR = ultraGrid1.Rows[iIndex];


            int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);

            if (e.Cell.Text == "1")
            {
                if (!m_dicSelectID2RowNo.Keys.Contains(nSelectFinRecvMoneyDetailTab_NewID))
                    m_dicSelectID2RowNo.Add(nSelectFinRecvMoneyDetailTab_NewID, iIndex);
            }
            else
            {
                if (m_dicSelectID2RowNo.Keys.Contains(nSelectFinRecvMoneyDetailTab_NewID))
                    m_dicSelectID2RowNo.Remove(nSelectFinRecvMoneyDetailTab_NewID);
            }
        }








    }


    internal class DateFilterCondition : FilterCondition
    {
        public DateFilterCondition(FilterCondition originalFilterCondition)
            : base(originalFilterCondition.Column, originalFilterCondition.ComparisionOperator, originalFilterCondition.CompareValue) { }

        public override bool MeetsCriteria(UltraGridRow row)
        {
            // If the CompareValue is not a DateTime, then just call the base implementation. 
            // This is to handle the "special" filter options like "(All)" or null.
            if ((this.CompareValue is DateTime) == false)
                return base.MeetsCriteria(row);

            DateTime compareDate = (DateTime)this.CompareValue;
            DateTime dtFirstDay = FirstDayOfMonth(compareDate);
            DateTime dtLastDay = LastDayOfMonth(compareDate);
            return dtFirstDay <= ((DateTime)row.Cells[this.Column].Value).Date && ((DateTime)row.Cells[this.Column].Value).Date <= dtLastDay;
        }

        private DateTime FirstDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day);
        }
        /// <summary>
        /// 取得某月的最后一天
        /// </summary>
        /// <param name="datetime">要取得月份最后一天的时间</param>
        /// <returns></returns>
        private DateTime LastDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
        }
    }
    public class CheckBoxOnHeader_CreationFilter : IUIElementCreationFilter
    {
        // This event will fire when the CheckBox is clicked. 
        public delegate void HeaderCheckBoxClickedHandler(object sender, HeaderCheckBoxEventArgs e);
        public event HeaderCheckBoxClickedHandler _CLICKED;
        public Dictionary<int, int> m_dicSelectID2RowNo = new Dictionary<int, int>();
        public Infragistics.Win.UltraWinGrid.UltraGrid m_ultraGrid1;
        public void SetParam(Dictionary<int, int> dicSelectID2RowNo, Infragistics.Win.UltraWinGrid.UltraGrid ultraGrid1)
        {
            m_dicSelectID2RowNo = dicSelectID2RowNo;
            m_ultraGrid1 = ultraGrid1;

        }
        public CheckBoxOnHeader_CreationFilter()
        {
            _CLICKED += new HeaderCheckBoxClickedHandler(aCheckBoxOnHeader_CreationFilter_HeaderCheckBoxClicked);
        }

        private void aCheckBoxOnHeader_CreationFilter_HeaderCheckBoxClicked(object sender, CheckBoxOnHeader_CreationFilter.HeaderCheckBoxEventArgs e)
        {
            // Check to see if the column is of type boolean.  If it is, set all the cells in that column to
            // whatever value the header checkbox is.
            if (e.Header.Column.Header.Caption == "全选")
            {
                //foreach (UltraGridRow aRow in e.Rows)
                //{
                //    aRow.Cells[e.Header.Column.Index].Value = (e.CurrentCheckState == CheckState.Checked);
                //}
                if (e.CurrentCheckState == CheckState.Checked)
                {
                    Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = m_ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                    if (ugr.Length <= 0)
                    {
                        return;
                    }

                    for (int i = 0; i < ugr.Length; i++)
                    {
                        int iIndex = ugr[i].Index;
                        UltraGridRow CurrentUGR = m_ultraGrid1.Rows[iIndex];
                        int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);

                        CurrentUGR.Cells["全选"].Value = 1;
                        if (!m_dicSelectID2RowNo.Keys.Contains(nSelectFinRecvMoneyDetailTab_NewID))
                            m_dicSelectID2RowNo.Add(nSelectFinRecvMoneyDetailTab_NewID, iIndex);


                    }
                }
                else
                {
                    foreach (int nID in m_dicSelectID2RowNo.Keys)
                    {
                        UltraGridRow CurrentUGR = m_ultraGrid1.Rows[m_dicSelectID2RowNo[nID]];
                        CurrentUGR.Cells["全选"].Value = 0;
                    }

                    m_dicSelectID2RowNo.Clear();
                }
            }
        }

        // EventArgs used for the HeaderCheckBoxClicked event. This event has to pass in the CheckState and the ColumnHeader
        #region HeaderCheckBoxEventArgs
        public class HeaderCheckBoxEventArgs : EventArgs
        {
            private Infragistics.Win.UltraWinGrid.ColumnHeader mvarColumnHeader;
            private CheckState mvarCheckState;
            private RowsCollection mvarRowsCollection;

            public HeaderCheckBoxEventArgs(Infragistics.Win.UltraWinGrid.ColumnHeader hdrColumnHeader, CheckState chkCheckState, RowsCollection Rows)
            {
                mvarColumnHeader = hdrColumnHeader;
                mvarCheckState = chkCheckState;
                mvarRowsCollection = Rows;
            }

            // Expose the rows collection for the specific row island that the header belongs to
            public RowsCollection Rows
            {
                get
                {
                    return mvarRowsCollection;
                }
            }

            public Infragistics.Win.UltraWinGrid.ColumnHeader Header
            {
                get
                {
                    return mvarColumnHeader;
                }
            }

            public CheckState CurrentCheckState
            {
                get
                {
                    return mvarCheckState;
                }
                set
                {
                    mvarCheckState = value;
                }
            }
        }
        #endregion

        private void aCheckBoxUIElement_ElementClick(Object sender, Infragistics.Win.UIElementEventArgs e)
        {
            // Get the CheckBoxUIElement that was clicked
            CheckBoxUIElement aCheckBoxUIElement = (CheckBoxUIElement)e.Element;

            // Get the Header associated with this particular element
            Infragistics.Win.UltraWinGrid.ColumnHeader aColumnHeader = (Infragistics.Win.UltraWinGrid.ColumnHeader)aCheckBoxUIElement.GetAncestor(typeof(HeaderUIElement)).GetContext(typeof(Infragistics.Win.UltraWinGrid.ColumnHeader));

            // Set the Tag on the Header to the new CheckState
            aColumnHeader.Tag = aCheckBoxUIElement.CheckState;

            // So that we can apply various changes only to the relevant Rows collection that the header belongs to
            HeaderUIElement aHeaderUIElement = aCheckBoxUIElement.GetAncestor(typeof(HeaderUIElement)) as HeaderUIElement;
            RowsCollection hRows = aHeaderUIElement.GetContext(typeof(RowsCollection)) as RowsCollection;

            // Raise an event so the programmer can do something when the CheckState changes
            if (_CLICKED != null)
                _CLICKED(this, new HeaderCheckBoxEventArgs(aColumnHeader, aCheckBoxUIElement.CheckState, hRows));
        }

        public bool BeforeCreateChildElements(Infragistics.Win.UIElement parent)  // Implements Infragistics.Win.IUIElementCreationFilter.BeforeCreateChildElements
        {
            // Don't need to do anything here
            return false;
        }

        public void AfterCreateChildElements(Infragistics.Win.UIElement parent) // Implements Infragistics.Win.IUIElementCreationFilter.AfterCreateChildElements
        {
            // Check for the HeaderUIElement
            if (parent is HeaderUIElement)
            {
                // Get the HeaderBase object from the HeaderUIElement
                Infragistics.Win.UltraWinGrid.HeaderBase aHeader = ((HeaderUIElement)parent).Header;

                // Only put the checkbox into headers whose DataType is boolean
                if (aHeader.Column.Header.Caption == "全选")
                {
                    TextUIElement aTextUIElement;
                    CheckBoxUIElement aCheckBoxUIElement = (CheckBoxUIElement)parent.GetDescendant(typeof(CheckBoxUIElement));

                    // Since the grid sometimes re-uses UIElements, we need to check to make sure 
                    // the header does not already have a CheckBoxUIElement attached to it.
                    // If it does, we just get a reference to the existing CheckBoxUIElement,
                    // and reset its properties.
                    if (aCheckBoxUIElement == null)
                    {
                        //Create a New CheckBoxUIElement
                        aCheckBoxUIElement = new CheckBoxUIElement(parent);
                    }

                    // Get the TextUIElement - this is where the text for the 
                    // Header is displayed. We need this so we can push it to the right
                    // in order to make room for the CheckBox
                    aTextUIElement = (TextUIElement)parent.GetDescendant(typeof(TextUIElement));

                    // Sanity check
                    if (aTextUIElement == null)
                        return;

                    // Get the Header and see if the Tag has been set. If the Tag is 
                    // set, we will assume it's the stored CheckState. This has to be
                    // done in order to maintain the CheckState when the grid repaints and
                    // UIElement are destroyed and recreated. 
                    Infragistics.Win.UltraWinGrid.ColumnHeader aColumnHeader =
                        (Infragistics.Win.UltraWinGrid.ColumnHeader)aCheckBoxUIElement.GetAncestor(typeof(HeaderUIElement))
                        .GetContext(typeof(Infragistics.Win.UltraWinGrid.ColumnHeader));

                    if (aColumnHeader.Tag == null)
                        //If the tag was nothing, this is probably the first time this 
                        //Header is being displayed, so default to Indeterminate
                        aColumnHeader.Tag = CheckState.Indeterminate;
                    else
                        aCheckBoxUIElement.CheckState = (CheckState)aColumnHeader.Tag;

                    // Hook the ElementClick of the CheckBoxUIElement
                    aCheckBoxUIElement.ElementClick += new UIElementEventHandler(aCheckBoxUIElement_ElementClick);

                    // Add the CheckBoxUIElement to the HeaderUIElement
                    parent.ChildElements.Add(aCheckBoxUIElement);

                    // Position the CheckBoxUIElement. The number 3 here is used for 3
                    // pixels of padding between the CheckBox and the edge of the Header.
                    // The CheckBox is shifted down slightly so it is centered in the header.
                    aCheckBoxUIElement.Rect = new Rectangle(parent.Rect.X + 3, parent.Rect.Y + ((parent.Rect.Height - aCheckBoxUIElement.CheckSize.Height) / 2), aCheckBoxUIElement.CheckSize.Width, aCheckBoxUIElement.CheckSize.Height);

                    // Push the TextUIElement to the right a little to make 
                    // room for the CheckBox. 3 pixels of padding are used again. 
                    aTextUIElement.Rect = new Rectangle(aCheckBoxUIElement.Rect.Right + 3, aTextUIElement.Rect.Y, parent.Rect.Width - (aCheckBoxUIElement.Rect.Right - parent.Rect.X), aTextUIElement.Rect.Height);
                }
                else
                {
                    // If the column is not a boolean column, we do not want to have a checkbox in it
                    // Since UIElements can be reused by the grid, there is a chance that one of the
                    // HeaderUIElements that we added a checkbox to for a boolean column header
                    // will be reused in a column that is not boolean.  In this case, we must remove
                    // the checkbox so that it will not appear in an inappropriate column header.
                    CheckBoxUIElement aCheckBoxUIElement = (CheckBoxUIElement)parent.GetDescendant(typeof(CheckBoxUIElement));

                    if (aCheckBoxUIElement != null)
                    {
                        parent.ChildElements.Remove(aCheckBoxUIElement);
                        aCheckBoxUIElement.Dispose();
                    }
                }
            }
        }
    }
}